我们在使用数据库连接时候,很多时候会考虑使用数据库连接池,因为数据库连接池的机制,使得在大用户数量上进行交互的时候使得其效率比较有优势!
http://blog.csdn.net/eclipser1987/article/details/5181320
- public class DBPool {
- private static DBPool dbPool;
- private ComboPooledDataSource dataSource;
- static {
- dbPool = new DBPool();
- }
- public DBPool() {
- try {
- dataSource = new ComboPooledDataSource();
- dataSource.setUser("root");
- dataSource.setPassword("123456");
- dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useUnicode=true");
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- // 设置初始连接池的大小!
- dataSource.setInitialPoolSize(2);
- // 设置连接池的最小值!
- dataSource.setMinPoolSize(1);
- // 设置连接池的最大值!
- dataSource.setMaxPoolSize(10);
- // 设置连接池中的最大Statements数量!
- dataSource.setMaxStatements(50);
- // 设置连接池的最大空闲时间!
- dataSource.setMaxIdleTime(60);
- } catch (PropertyVetoException e) {
- throw new RuntimeException(e);
- }
- }
- public final static DBPool getInstance() {
- return dbPool;
- }
- public final Connection getConnection() {
- try {
- return dataSource.getConnection();
- } catch (SQLException e) {
- throw new RuntimeException("无法从数据源获取连接 ", e);
- }
- }
- public static void main(String[] args) throws SQLException {
- Connection con = null;
- try {
- con = DBPool.getInstance().getConnection();
- ResultSet rs = con.createStatement().executeQuery("SELECT * FROM LESOGO_USER");
- while (rs.next()) {
- System.out.println(rs.getObject(1));
- System.out.println(rs.getObject(2));
- System.out.println(rs.getObject(3));
- }
- } catch (Exception e) {
- } finally {
- if (con != null)
- con.close();
- }
- }
- }
- /**
- * 此示例演示如何获得C3P0的数据源并将其绑定到一个JNDI名称服务。
- */
- public final class JndiBindDataSource {
- /**
- * 一定要载入数据库驱动程序类,要么通过Class.forName()[如下]或外部显示(例如,通过使用- Djdbc.drivers启动时你的JVM)。
- */
- static {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] argv) {
- try {
- /**
- * 让一个命令行参数指定名称,将我们的数据源的绑定。
- */
- String jndiName = argv[0];
- /**
- * 获取数据源使用预设池params ...
- * 这是唯一的C3P0的特定代码在这里
- */
- DataSource unpooled = DataSources
- .unpooledDataSource("jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useUnicode=true", "root", "123456");
- DataSource pooled = DataSources.pooledDataSource(unpooled);
- System.out.println("是否能够获得Connection : "+pooled.getConnection());
- /**
- * 创建的InitialContext,并绑定的数据源,以它在通常的方式。
- * 我们使用的是没有的的InitialContext的构造参数版本,因此通过jndi.properties的文件,
- * 系统属性,或通过其他手段,JNDI环境,必须先设置。
- */
- InitialContext ctx = new InitialContext();
- System.out.println("jndiName : " + jndiName);
- ctx.rebind(jndiName, pooled);
- System.out.println("DataSource bound to nameservice under the name /"" + jndiName + '/"');
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(ResultSet o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Statement o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Connection o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private JndiBindDataSource() {
- }
- }
- /**
- * 此示例演示如何以编程方式,直接使用unpooled数据源
- */
- public final class UseJndiDataSource {
- public static void main(String[] argv) {
- try {
- /**
- * 让一个命令行参数指定名称,我们将查找的数据源。
- */
- String jndiName = argv[0];
- /**
- * 创建InitialContext的,和查找惯常做法的DataSource。 我们使用的是没有的的InitialContext的构造参数版本,因此通过jndi.properties的文件,
- * 系统属性,或通过其他手段,JNDI环境,必须先设置。
- */
- InitialContext ctx = new InitialContext();
- /**
- * 获取数据源...这是唯一的C3P0的特定代码在这里
- */
- DataSource ds = (DataSource) ctx.lookup(jndiName);
- /**
- * 掌握了这样一个连接东西,通常的方式
- */
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- con = ds.getConnection();
- stmt = con.createStatement();
- rs = stmt.executeQuery("SELECT * FROM LESOGO_USER");
- while (rs.next())
- System.out.println(rs.getString(1));
- } finally {
- /**
- * 我试图约在资源管理,`神经质显式关闭每个资源,但如果你只收盘家长资源的习惯(如连接), 让他们接近自己的孩子,都C3P0的数据源一定会妥善处理。
- */
- attemptClose(rs);
- attemptClose(stmt);
- attemptClose(con);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(ResultSet o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Statement o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Connection o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private UseJndiDataSource() {
- }
- }
- /**
- * 此示例演示如何以编程方式,直接使用数据库连接池中的数据源
- */
- public final class UsePoolBackedDataSource {
- public static void main(String[] argv) {
- try {
- //
- // 收购之前,您的数据源!
- // 获取数据源...这是唯一的C3P0的特定代码在这里
- DataSource unpooled = DataSources.unpooledDataSource(
- "jdbc:mysql://127.0.0.1:3306/lesogo_game2?user=root&password=123456&useUnicode=true", "root",
- "123456");
- DataSource pooled = DataSources.pooledDataSource(unpooled);
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- con = pooled.getConnection();
- stmt = con.createStatement();
- rs = stmt.executeQuery("SELECT * FROM LESOGO_USER");
- while (rs.next())
- System.out.println(rs.getString(1));
- } finally {
- attemptClose(rs);
- attemptClose(stmt);
- attemptClose(con);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(ResultSet o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Statement o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- static void attemptClose(Connection o) {
- try {
- if (o != null)
- o.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private UsePoolBackedDataSource() {
- }
- }
从网上看到的一个关于数据库连接在使用了C3P0前后的比较。本人暂时还没有测试过。
下面是一个C3P0的工具类。
- import java.beans.PropertyVetoException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public final class ConnectionManager {
- private static ConnectionManager instance;
- private static ComboPooledDataSource dataSource;
- private ConnectionManager() throws SQLException, PropertyVetoException {
- dataSource = new ComboPooledDataSource();
- dataSource.setUser("loux");
- dataSource.setPassword("loux");
- dataSource.setJdbcUrl("jdbc:oracle:thin:@192.168.100.70:1521:orcl");
- dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
- dataSource.setInitialPoolSize(5);
- dataSource.setMinPoolSize(1);
- dataSource.setMaxPoolSize(10);
- dataSource.setMaxStatements(50);
- dataSource.setMaxIdleTime(60);
- }
- public static final ConnectionManager getInstance() {
- if (instance == null) {
- try {
- instance = new ConnectionManager();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return instance;
- }
- public synchronized final Connection getConnection() {
- Connection conn = null;
- try {
- conn = dataSource.getConnection();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public final class ConnectionManager {
private static ConnectionManager instance;
private static ComboPooledDataSource dataSource;
private ConnectionManager() throws SQLException, PropertyVetoException {
dataSource = new ComboPooledDataSource();
dataSource.setUser("loux");
dataSource.setPassword("loux");
dataSource.setJdbcUrl("jdbc:oracle:thin:@192.168.100.70:1521:orcl");
dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
dataSource.setInitialPoolSize(5);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
}
public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
public synchronized final Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
接下来是数据库使用了c3p0连接池和没有使用c3p0的连接操作。
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import oracle.jdbc.pool.OracleDataSource;
- public class ConnectionDemo {
- public static void main(String[] args) throws SQLException {
- System.out.println("使用连接池................................");
- for (int i = 0; i < 20; i++) {
- long beginTime = System.currentTimeMillis();
- Connection conn = ConnectionManager.getInstance().getConnection();
- try {
- PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM t_fmscpy200");
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
- }
- System.out.println("不使用连接池................................");
- for (int i = 0; i < 20; i++) {
- long beginTime = System.currentTimeMillis();
- OracleDataSource ods = new OracleDataSource();
- ods.setUser("loux");
- ods.setPassword("loux");
- ods.setURL("jdbc:oracle:thin:@192.168.100.70:1521:orcl");
- Connection conn = ods.getConnection();
- try {
- PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table_name");
- ResultSet rs = pstmt.executeQuery();
- while (rs.next()) {
- // do nothing...
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("第" + (i + 1) + "次执行花费时间为:"
- + (endTime - beginTime));
- }
- }
- }
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
public class ConnectionDemo {
public static void main(String[] args) throws SQLException {
System.out.println("使用连接池................................");
for (int i = 0; i < 20; i++) {
long beginTime = System.currentTimeMillis();
Connection conn = ConnectionManager.getInstance().getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM t_fmscpy200");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
}
System.out.println("不使用连接池................................");
for (int i = 0; i < 20; i++) {
long beginTime = System.currentTimeMillis();
OracleDataSource ods = new OracleDataSource();
ods.setUser("loux");
ods.setPassword("loux");
ods.setURL("jdbc:oracle:thin:@192.168.100.70:1521:orcl");
Connection conn = ods.getConnection();
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM table_name");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
// do nothing...
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("第" + (i + 1) + "次执行花费时间为:"
+ (endTime - beginTime));
}
}
}
最后是控制台输出:
- 使用连接池................................
- 第1次执行花费时间为:1469
- 第2次执行花费时间为:0
- 第3次执行花费时间为:16
- 第4次执行花费时间为:0
- 第5次执行花费时间为:0
- 第6次执行花费时间为:15
- 第7次执行花费时间为:0
- 第8次执行花费时间为:0
- 第9次执行花费时间为:0
- 第10次执行花费时间为:0
- 第11次执行花费时间为:16
- 第12次执行花费时间为:0
- 第13次执行花费时间为:0
- 第14次执行花费时间为:0
- 第15次执行花费时间为:0
- 第16次执行花费时间为:16
- 第17次执行花费时间为:0
- 第18次执行花费时间为:0
- 第19次执行花费时间为:15
- 第20次执行花费时间为:0
- 不使用连接池................................
- 第1次执行花费时间为:47
- 第2次执行花费时间为:31
- 第3次执行花费时间为:32
- 第4次执行花费时间为:46
- 第5次执行花费时间为:32
- 第6次执行花费时间为:31
- 第7次执行花费时间为:47
- 第8次执行花费时间为:31
- 第9次执行花费时间为:47
- 第10次执行花费时间为:31
- 第11次执行花费时间为:47
- 第12次执行花费时间为:31
- 第13次执行花费时间为:32
- 第14次执行花费时间为:46
- 第15次执行花费时间为:47
- 第16次执行花费时间为:32
- 第17次执行花费时间为:46
- 第18次执行花费时间为:47
- 第19次执行花费时间为:32
- 第20次执行花费时间为:31
使用连接池................................ 第1次执行花费时间为:1469 第2次执行花费时间为:0 第3次执行花费时间为:16 第4次执行花费时间为:0 第5次执行花费时间为:0 第6次执行花费时间为:15 第7次执行花费时间为:0 第8次执行花费时间为:0 第9次执行花费时间为:0 第10次执行花费时间为:0 第11次执行花费时间为:16 第12次执行花费时间为:0 第13次执行花费时间为:0 第14次执行花费时间为:0 第15次执行花费时间为:0 第16次执行花费时间为:16 第17次执行花费时间为:0 第18次执行花费时间为:0 第19次执行花费时间为:15 第20次执行花费时间为:0 不使用连接池................................ 第1次执行花费时间为:47 第2次执行花费时间为:31 第3次执行花费时间为:32 第4次执行花费时间为:46 第5次执行花费时间为:32 第6次执行花费时间为:31 第7次执行花费时间为:47 第8次执行花费时间为:31 第9次执行花费时间为:47 第10次执行花费时间为:31 第11次执行花费时间为:47 第12次执行花费时间为:31 第13次执行花费时间为:32 第14次执行花费时间为:46 第15次执行花费时间为:47 第16次执行花费时间为:32 第17次执行花费时间为:46 第18次执行花费时间为:47 第19次执行花费时间为:32 第20次执行花费时间为:31可以看出,在使用连接池时,第一次执行花费的时间稍长,因为第一次初始化操作需要创建多个连接并放入池中,以后使用时将会大大缩短执行时间。
在不使用连接池时,每次花费的时间都比较长。
转载于:http://topic.csdn.net/u/20070702/21/82749d1e-0746-415e-b7da-1ddfb3c885f5.html