1.Dao.java
package com.tothinkgames.t1.db.gm;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.jolbox.bonecp.BoneCPConfig;
import com.tothinkgames.t1.config.common.AreaUrlsConfig;
import com.tothinkgames.t1.config.gm.GMServerInstance;
import com.tothinkgames.t1.util.Env;
import com.tothinkgames.util.db.BoneCPDatabase;
import com.tothinkgames.util.db.Database;
public class Dao {
private static Database db;
public static Database getDb() {
return db;
}
public static void prepareBoneCPDb() throws Exception {
if (db == null) {
BoneCPConfig config = new BoneCPConfig(new FileInputStream(Env.getConfigFile("database_bonecp.xml", "GMServer", GMServerInstance.getAreaId())),
"used");
config.setJdbcUrl(AreaUrlsConfig.getGmDbUrl());
config.setUsername(AreaUrlsConfig.getGmDbUsername());
config.setPassword(AreaUrlsConfig.getGmDbPassword());
db = new BoneCPDatabase(config);
}
}
public static void releaseConnectionFinally(Connection conn) {
try {
getDb().releaseConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void rollbackConnectionExceptionally(Connection conn) {
try {
if (conn != null) {
conn.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getBillingServerConnection(boolean autoCommit) throws SQLException {
Connection conn = DriverManager.getConnection(AreaUrlsConfig.getBillingDbUrl(), AreaUrlsConfig.getBillingDbUsername(),
AreaUrlsConfig.getBillingDbPassword());
conn.setAutoCommit(autoCommit);
return conn;
}
}
2.Database.java
package com.tothinkgames.util.db;
import java.sql.Connection;
import java.sql.SQLException;
public interface Database {
Connection allocateConnection() throws SQLException;
Connection allocateConnection(boolean autoCommit) throws SQLException;
void releaseConnection(Connection conn) throws SQLException;
}
3.BoneCPDatabase.java
package com.tothinkgames.util.db;
import java.sql.Connection;
import java.sql.SQLException;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
public class BoneCPDatabase implements Database {
private static final String JdbcDriver = "com.mysql.jdbc.Driver";
private BoneCP pool;
public BoneCPDatabase(BoneCPConfig config) throws ClassNotFoundException, SQLException {
Class.forName(JdbcDriver);
pool = new BoneCP(config);
}
protected void finalize() throws Throwable {
pool.shutdown();
super.finalize();
}
@Override
public Connection allocateConnection() throws SQLException {
return allocateConnection(false);
}
@Override
public Connection allocateConnection(boolean autoCommit) throws SQLException {
Connection conn = pool.getConnection();
conn.setAutoCommit(autoCommit);
return conn;
}
@Override
public void releaseConnection(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
}
4.database_bonecp.xml
<?xml version="1.0" encoding="UTF-8"?>
<bonecp-config>
<default-config/>
<named-config name="used">
<!-- 设置分区个数。这个参数默认为1,建议3-4(根据特定应用程序而定)。
为了减少锁竞争和改善性能,从当前线程分区(thread-affinity)中获取一个connection, 也就是这个样子:partitions[Thread.currentThread().getId() % partitionCount]。当拥有充足的短期(short-lived)的线程时候,这个参数设置越大,性能越好。当超过一定的阀值时,连接池的维护工作就可能对性能造成一定的负面影响(仅当分区上的connection使用耗尽时)。 -->
<property name="partitionCount">1</property>
<!-- 设置每个分区含有connection最大个数。这个参数默认为2。如果小于2,BoneCP将设置为50。
比如:partitionCount设置为3,maxConnectionPerPartition设置为5,你就会拥有总共15个connection。
注意:BoneCP不会将这些connection一起创建出来,而是说在需要更多connection的时候从minConnectionsPerPartition参数开始逐步地增长connection数量。 -->
<property name="maxConnectionsPerPartition">2</property>
<!-- 设置每个分区含有connection最大小个数。这个参数默认为0。 -->
<property name="minConnectionsPerPartition">1</property>
<!-- 设置分区中的connection增长数量。这个参数默认为1。
当每个分区中的connection大约快用完时,BoneCP动态批量创建connection。
这个属性控制一起创建多少个connection(不会大于maxConnectionsPerPartition)。
注意:这个配置属于每个分区的设置。 -->
<property name="acquireIncrement">3</property>
<!-- 设置连接池阀值。这个参数默认为20。如果小于0或是大于100,BoneCP将设置为20。
连接池观察线程(PoolWatchThread)试图为每个分区维护一定数量的可用connection。
这个数量趋于maxConnectionPerPartition和minConnectionPerPartition之间。这个参数是以百分比的形式来计算的。例如:设置为20,下面的条件如果成立:Free Connections / MaxConnections < poolAvailabilityThreshold;就会创建出新的connection。
换句话来说连接池为每个分区至少维持20%数量的可用connection。
设置为0时,每当需要connection的时候,连接池就要重新创建新connection,这个时候可能导致应用程序可能会为了获得新connection而小等一会。 -->
<property name="poolAvailabilityThreshold">20</property>
<!-- 设置获取connection超时的时间。这个参数默认为Long.MAX_VALUE。单位:毫秒。
在调用getConnection获取connection时,获取时间超过了这个参数,就视为超时并报异常。 -->
<property name="connectionTimeoutInMs">10000</property>
</named-config>
</bonecp-config>
5.参考:bonecp配置参数: http://chirs1012f.iteye.com/blog/805261