欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C3P0

程序员文章站 2022-07-13 09:33:51
...

在项目开发中,使用c3p0 操作数据库连接池,其中需要在数据库用户下创建c3p0这个测试表

如配置:

DB_TEST_TABLENAME=C3P0

实现数据库驱动连接配置获取:

try{
   ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
   pooledDataSource.setDriverClass(DB_DRIVER_CLASS);
   pooledDataSource.setJdbcUrl(DB_JDBC_URL);
   pooledDataSource.setUser(DB_USER);
   pooledDataSource.setPassword(DB_PASSWORD);
   pooledDataSource.setMinPoolSize(DB_MIN_POOLSIZE);
   pooledDataSource.setMaxPoolSize(DB_MAX_POOLSIZE);
   pooledDataSource.setInitialPoolSize(DB_INITIAL_POOLSIZE);

   // 连接关闭时默认将所有未提交的操作回滚
   pooledDataSource.setAutoCommitOnClose(false);
   // 每60秒检查所有连接池中的空闲连接。Default: 0
   pooledDataSource.setIdleConnectionTestPeriod(60);
   // 最大空闲时间,600秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0
   pooledDataSource.setMaxIdleTime(600);
   //当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 acquireIncrement
   pooledDataSource.setAcquireIncrement(5);
   pooledDataSource.setAutomaticTestTable(DB_TEST_TABLENAME);//c3p0
   
   javax.naming.Context ctx=new InitialContext();
   ctx.bind(Constants.JDBC_JNDI, pooledDataSource);

 

  实现连接数据库:

public static Connection getConnection() {
  Connection   conn=null;
  try {
   Context ctx=new InitialContext();
   javax.sql.DataSource ds=(javax.sql.DataSource)ctx.lookup(Constants.JDBC_JNDI);
   conn=ds.getConnection();
  }catch(Exception e) {
   e.printStackTrace();
  }
  return conn;
 }

注意问题:ORA-00942: 表或视图不存在 ---------缺少c3p0的测试表导致

相关标签: c3p0