ActiveJDBC 有 SQLSyntaxErrorException

ActiveJDBC has SQLSyntaxErrorException

使用 ActiveJDBC,我一直在手动打开和关闭连接、插入和读取任何内容,所有这些都没有像这样的错误:

public class DatabaseLoader {

    private static final Logger logger = LogManager.getLogger(DatabaseLoader.class);

    public static void openConnection() {
        Base.open("com.mysql.cj.jdbc.Driver", ConfigManager.getAddress(), ConfigManager.getUsername(), ConfigManager.getPassword());
    }

    public static void openConnectionIfClosed() {

        if (!Base.hasConnection()) {
            logger.info("Connecting to database!");
            openConnection();
        }
    }

    public static void closeConnectionIfOpen() {
        if (Base.hasConnection()) {
            logger.info("Disconnecting from database!");
            Base.close();
        }
    }
}

不过,我最近尝试添加 C3P0 和连接池,尽可能紧跟 https://github.com/javalite/javalite/blob/master/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java。新代码如下所示:

public class DatabaseLoader {

    private static final Logger logger = LogManager.getLogger(DatabaseLoader.class);

    static DataSource dataSourceUnpooled;
    static DataSource dataSourcePooled;

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            dataSourceUnpooled = DataSources.unpooledDataSource(ConfigManager.getAddress(), ConfigManager.getUsername(), ConfigManager.getPassword());
            dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled); //init the connection pool
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void openConnection() {
        Base.open(dataSourcePooled);
    }

    public static void openConnectionIfClosed() {
        if (!Base.hasConnection()) {
            openConnection();
        }
    }

    public static void closeConnectionIfOpen() {
        if (Base.hasConnection()) {
            logger.info("Disconnecting from database!");
            Base.close();
        }
    }
}

使用此代码,与数据库的初始连接似乎成功并且一切正常,直到我插入一些东西。例如,像这样:

// ServerMessage is an activejdbc model that has worked to insert things 31k times before adding C3P0

ServerMessage message = ServerMessage.findOrCreateIt("message_id_snowflake", messageIdSnowflake, "server_id", serverId, "user_id", userId);

每当我插入一些东西时,我现在都会收到这个错误:

org.javalite.activejdbc.DBException: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '::BIGINT,19::INT,1772::INT)' at line 1, query: INSERT INTO server_messages (message_id_snowflake, server_id, user_id) VALUES (?::BIGINT,?::INT,?::INT), params: 928224895626793001, 19, 1772
        at org.javalite.activejdbc.DB.execInsert(DB.java:734)
        at org.javalite.activejdbc.Model.insert(Model.java:2741)
        at org.javalite.activejdbc.Model.save(Model.java:2673)
        at org.javalite.activejdbc.Model.saveIt(Model.java:2597)
        at org.javalite.activejdbc.ModelDelegate.createIt(ModelDelegate.java:146)
        at org.javalite.activejdbc.ModelDelegate.findOrCreateIt(ModelDelegate.java:438)
        at org.javalite.activejdbc.ModelDelegate.findOrCreateIt(ModelDelegate.java:412)
        at dev.laarryy.eris.models.guilds.ServerMessage.findOrCreateIt(ServerMessage.java:219)
        at dev.laarryy.eris.listeners.MessageCreateListener.on(MessageCreateListener.java:49)
        ... 21 more
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '::BIGINT,19::INT,1772::INT)' at line 1
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1348)
        at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:1502)
        at org.javalite.activejdbc.DB.execInsert(DB.java:713)
        ... 29 more

这对我来说似乎很奇怪,因为应该插入的 bigint、int 和 int 确实是所需的 bigint、int 和 int,我不知道我还能做些什么才能找到错误的原因。堆栈跟踪倒数第二行的 C3P0 行让我认为 ActiveJDBC 准备的语句在某种程度上不是将其发送到数据库的语句。

我也已经尝试启用 C3P0 语句缓存,以防有效果,但没有。

如有任何建议或指导,我们将不胜感激!

更新

我现在也尝试按照此处的快速启动程序 https://www.mchange.com/projects/c3p0/#quickstart 进行操作,希望手动设置驱动程序 class 可以解决问题,但事实并非如此。我仍然得到相同的堆栈跟踪。我试过的代码:

public class DatabaseLoader {

    private static final Logger logger = LogManager.getLogger(DatabaseLoader.class);

    static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

    static {
        try {
           comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            comboPooledDataSource.setJdbcUrl(ConfigManager.getAddress());
            comboPooledDataSource.setUser(ConfigManager.getUsername());
            comboPooledDataSource.setPassword(ConfigManager.getPassword());
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public static void openConnection() {
        Base.open(comboPooledDataSource);
    }

    public static void openConnectionIfClosed() {
        if (!Base.hasConnection()) {
            openConnection();
        }
    }

    public static void closeConnectionIfOpen() {
        if (Base.hasConnection()) {
            logger.info("Disconnecting from database!");
            Base.close();
        }
    }
}

我也曾尝试切换到 Hikari 版本 5,我得到了完全相同的错误,除了倒数第二行来自 Hikari 而不是 c3p0。这让我相信 ActiveJDBC 端有问题,而不是连接池端。

请求的详细信息:

不幸的是,发布了几个小时的有缺陷的快照。该快照 v 3.0-SNAPSHOT 有一个错误,其中 PostgreSQL 语法被意外设为 MySQL 的默认语法。看来是给你惹了麻烦。 JDBC 连接池与此无关。我们对快照很小心,但这样的事情会发生(几年一次)。正好在更新的中间,你真倒霉。

解决这个问题的方法是清除所有 Maven 缓存:

rm -rf ~/.m2/repository/org/javalite

在下一个版本中,您将获得一个没有此缺陷的新快照(大大改进:))。

抱歉给您带来麻烦,快照“正在进行中”。

相关错误:https://github.com/javalite/javalite/issues/1187