Using HikariCP + Pet issues + Dojo skills
As Suggested by Alex-0000, migrated from DBCP to HikariCP (uses less external JARs and got a cleaner code overall). Fixed some issues regarding pets, such as evolving or hatching from egg. Added dojo skills and fixed energy bar. Added missing upgrade books on drop data.
This commit is contained in:
@@ -3,12 +3,8 @@ package tools;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
import org.apache.commons.dbcp2.ConnectionFactory;
|
||||
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
|
||||
import org.apache.commons.dbcp2.PoolableConnectionFactory;
|
||||
import org.apache.commons.dbcp2.PoolingDriver;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
import constants.ServerConstants;
|
||||
|
||||
@@ -18,74 +14,43 @@ import constants.ServerConstants;
|
||||
* @author Ronan (some connection pool to this beautiful code)
|
||||
*/
|
||||
public class DatabaseConnection {
|
||||
private static Properties prop;
|
||||
private static HikariDataSource ds;
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
if(prop != null) {
|
||||
Connection con = null;
|
||||
while(con == null) { //oh yes, this will loop until success!
|
||||
try {
|
||||
con = DriverManager.getConnection(ServerConstants.DB_URL, prop);
|
||||
} catch (SQLException sqle) {}
|
||||
}
|
||||
|
||||
return con;
|
||||
if(ds != null) {
|
||||
return ds.getConnection();
|
||||
} else {
|
||||
return DriverManager.getConnection(ServerConstants.DB_URL, ServerConstants.DB_USER, ServerConstants.DB_PASS);
|
||||
}
|
||||
}
|
||||
|
||||
private static Properties getProperties() {
|
||||
Properties connectionProperties = new Properties();
|
||||
connectionProperties.put("user", ServerConstants.DB_USER);
|
||||
connectionProperties.put("password", ServerConstants.DB_PASS);
|
||||
|
||||
connectionProperties.put("minIdle", "-1");
|
||||
connectionProperties.put("maxIdle", "-1");
|
||||
|
||||
connectionProperties.put("testOnBorrow", "true");
|
||||
connectionProperties.put("lifo", "false");
|
||||
|
||||
connectionProperties.put("maxTotal", "42"); //max allotted connections
|
||||
connectionProperties.put("maxConnLifetimeMillis", "60000"); //connection remains valid for 1 min
|
||||
connectionProperties.put("maxWaitMillis", "777"); //there are more pools, if this one is unavailable then pass to another already
|
||||
connectionProperties.put("poolPreparedStatements", "true");
|
||||
connectionProperties.put("maxOpenPreparedStatements", "100"); //max allotted PS
|
||||
|
||||
return connectionProperties;
|
||||
}
|
||||
|
||||
private PoolingDriver installDriver(short id) {
|
||||
GenericObjectPool connectionPool = new GenericObjectPool(null);
|
||||
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(ServerConstants.DB_URL, prop);
|
||||
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool.getJmxName());
|
||||
PoolingDriver driver = new PoolingDriver();
|
||||
driver.registerPool("maplesolaxiapool" + id, connectionPool);
|
||||
|
||||
return driver;
|
||||
}
|
||||
|
||||
public DatabaseConnection() {
|
||||
prop = null;
|
||||
ds = null;
|
||||
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver"); // touch the mysql driver
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println("[SEVERE] SQL Driver Not Found. Consider death by clams.");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
if(ServerConstants.DB_EXPERIMENTAL_POOLS > 0) {
|
||||
if(ServerConstants.DB_EXPERIMENTAL_POOL) {
|
||||
// Connection Pool on database ftw!
|
||||
|
||||
prop = getProperties();
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setJdbcUrl(ServerConstants.DB_URL);
|
||||
|
||||
config.setUsername(ServerConstants.DB_USER);
|
||||
config.setPassword(ServerConstants.DB_PASS);
|
||||
|
||||
config.addDataSourceProperty("connectionTimeout", "30000");
|
||||
config.addDataSourceProperty("maximumPoolSize", "100");
|
||||
|
||||
config.addDataSourceProperty("cachePrepStmts", "true");
|
||||
config.addDataSourceProperty("prepStmtCacheSize", "250");
|
||||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
|
||||
|
||||
ds = new HikariDataSource(config);
|
||||
} else {
|
||||
try {
|
||||
for(short i = 0; i < ServerConstants.DB_EXPERIMENTAL_POOLS; i++) {
|
||||
DriverManager.registerDriver(installDriver(i));
|
||||
}
|
||||
} catch(SQLException sqle) {
|
||||
sqle.printStackTrace();
|
||||
Class.forName("com.mysql.jdbc.Driver"); // touch the mysql driver
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.out.println("[SEVERE] SQL Driver Not Found. Consider death by clams.");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user