ORMLite MySQL initialization doesn't seem to work

618 views Asked by At

I've been trying to create an ORMLite JdbcPooledConnectionSource to connect to a database.

This is the code where I create it:

    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);
    }

And this is where I use it. All these 'Storages' implement BaseDaoImpl and pass the connection to their super constructor:

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);
}

However, I get this exception:

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

This is the exception I don't understand:

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

because I cleary did initialize the connection!

connection.initialize();

Or is it something else I'm doing wrong?

1

There are 1 answers

0
theblackips On BEST ANSWER

Okay, I figured it out myself...

Apparently you have to call initialize() not only for the connection but also for the BaseDaoImpl.