繁体   English   中英

ORMLite MySQL初始化似乎不起作用

[英]ORMLite MySQL initialization doesn't seem to work

我一直在尝试创建一个ORMLite JdbcPooledConnectionSource以连接到数据库。

这是我创建它的代码:

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    String url = String.format("jdbc:%s%s%s%s/%s",
            hostname.startsWith("mysql://") ? "" : "mysql://", hostname,
            port != "" ? ":" : "", port, databaseName);

    try {
        JdbcPooledConnectionSource connection = new JdbcPooledConnectionSource(
                url, username, password);

        connection.setMaxConnectionsFree(config.getInt(database
                + ".connections", 1));

        connection.setTestBeforeGet(false);
        connection.setMaxConnectionAgeMillis(900000);
        connection.setCheckConnectionsEveryMillis(0);
        connection.setDatabaseType(new MysqlDatabaseType());
        connection.initialize();

        return connection;
    } catch (SQLException e) {
        throw new ConnectionException(e);
    }

这就是我使用它的地方。 所有这些“存储”都实现BaseDaoImpl并将连接传递给其super构造函数:

public static void setupStorage() throws SQLException {
    if(connection == null){
        System.err.println("Could not set up storage. No connections avaliable.");
        return;
    }
    playerBanStorage = new PlayerBanStorage(connection);
    playerKickStorage = new PlayerKickStorage(connection);
    playerMuteStorage = new PlayerMuteStorage(connection);
    playerStorage = new PlayerStorage(connection);
    playerWarnStorage = new PlayerWarnStorage(connection);
    ipBanStorage = new IpBanStorage(connection);
    playerBanRecordStorage = new PlayerBanRecordStorage(connection);
    ipBanRecordStorage = new IpBanRecordStorage(connection);
    playerMuteRecordStorage = new PlayerMuteRecordStorage(connection);
}

但是,我得到此异常:

18:13:10 [Severe] java.sql.SQLException: Could not call the constructor in class class de.tbi.bans.storage.PlayerStorage
18:13:10 [Severe]     at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
18:13:10 [Severe]     at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:95)
18:13:10 [Severe]     at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:297)
18:13:10 [Severe]     at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:201)
18:13:10 [Severe]     at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
18:13:10 [Severe]     at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
18:13:10 [Severe]     at de.tbi.bans.storage.IpBanStorage.<init>(IpBanStorage.java:29)
18:13:10 [Severe]     at de.tbi.bans.Bans.setupStorage(Bans.java:50)
18:13:10 [Severe]     at de.tbi.bans.runnables.Setup.run(Setup.java:11)
18:13:10 [Severe]     at net.md_5.bungee.scheduler.BungeeTask.run(BungeeTask.java:63)
18:13:10 [Severe]     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
18:13:10 [Severe]     at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
18:13:10 [Severe]     at java.lang.Thread.run(Unknown Source)
18:13:10 [Severe] Caused by: java.lang.reflect.InvocationTargetException
18:13:10 [Severe]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
18:13:10 [Severe]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
18:13:10 [Severe]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
18:13:10 [Severe]     at java.lang.reflect.Constructor.newInstance(Unknown Source)
18:13:10 [Severe]     at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:92)
18:13:10 [Severe]     ... 11 more
18:13:10 [Severe] Caused by: java.lang.IllegalStateException: you must call initialize() before you can use the dao
18:13:10 [Severe]     at com.j256.ormlite.dao.BaseDaoImpl.checkForInitialized(BaseDaoImpl.java:927)
18:13:10 [Severe]     at com.j256.ormlite.dao.BaseDaoImpl.isTableExists(BaseDaoImpl.java:688)
18:13:10 [Severe]     at de.tbi.bans.storage.PlayerStorage.<init>(PlayerStorage.java:39)
18:13:10 [Severe]     ... 16 more

这是我不明白的例外:

java.lang.IllegalStateException: you must call initialize() before you can use the dao

因为我明确地初始化了连接!

connection.initialize();

还是我做错了什么?

好吧,我自己弄清楚了...

显然,您不仅必须为连接调用initialize() ,而且还必须调用BaseDaoImpl

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM