[英]ORMLite MySQL initialization doesn't seem to work
I've been trying to create an ORMLite JdbcPooledConnectionSource
to connect to a database. 我一直在尝试创建一个ORMLite JdbcPooledConnectionSource
以连接到数据库。
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: 所有这些“存储”都实现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);
}
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? 还是我做错了什么?
Okay, I figured it out myself... 好吧,我自己弄清楚了...
Apparently you have to call initialize()
not only for the connection but also for the BaseDaoImpl
. 显然,您不仅必须为连接调用initialize()
,而且还必须调用BaseDaoImpl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.