/** * 从连接池中取出链接。 * * @return */ public synchronized Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { System.exit(0); } return null; }
static void doSomething(DataSource ds) throws SQLException { Connection con = null; Statement stmt = null; try { con = ds.getConnection(); stmt = con.createStatement(); int i = stmt.executeUpdate( "INSERT INTO TRSS_TABLE VALUES ('" + System.currentTimeMillis() + "')"); if (i != 1) throw new SQLException("Insert failed somehow strange!"); } finally { StatementUtils.attemptClose(stmt); ConnectionUtils.attemptClose(con); } }
public synchronized void close() { resetPoolManager(); is_closed = true; C3P0Registry.markClosed(this); if (Debug.DEBUG && Debug.TRACE == Debug.TRACE_MAX && logger.isLoggable(MLevel.FINEST)) { logger.log( MLevel.FINEST, this.getClass().getName() + '@' + Integer.toHexString(System.identityHashCode(this)) + " has been closed. ", new Exception("DEBUG STACK TRACE for PoolBackedDataSource.close().")); } }
/** * 设置数据源参数。<br> * <B>注:</B>该方法必须在使用数据库之前调用,当连接池无法加载驱动时,程序已经没有必要再继续运行了,所以<br> * ; 这里采用强制退出的方式来关闭程序。 * * @throws SQLException * @throws PropertyVetoException */ public ConnectionPool() { dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass("com.mysql.jdbc.Driver"); } catch (PropertyVetoException e) { System.exit(0); } dataSource.setJdbcUrl( "jdbc:mysql://localhost:3306/bbsdb?autoReconnect=true&characterEncoding=gbk"); dataSource.setUser("root"); dataSource.setPassword("111"); dataSource.setInitialPoolSize(10); dataSource.setMinPoolSize(5); dataSource.setMaxStatements(50); dataSource.setMaxPoolSize(50); dataSource.setMaxIdleTime(0); dataSource.setAcquireIncrement(5); }
public static void main(String[] argv) { try { ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setJdbcUrl(argv[0]); cpds.setUser(argv[1]); cpds.setPassword(argv[2]); cpds.setMinPoolSize(5); cpds.setAcquireIncrement(5); cpds.setMaxPoolSize(20); System.err.println("Initial..."); display(cpds); Thread.sleep(2000); HashSet hs = new HashSet(); for (int i = 0; i < 20; ++i) { Connection c = cpds.getConnection(); hs.add(c); System.err.println("Adding (" + (i + 1) + ") " + c); display(cpds); Thread.sleep(1000); // if (i == 9) // { // //System.err.println("hardReset()ing"); // //cpds.hardReset(); // System.err.println("softReset()ing"); // cpds.softReset(); // } } int count = 0; for (Iterator ii = hs.iterator(); ii.hasNext(); ) { Connection c = ((Connection) ii.next()); System.err.println("Removing " + ++count); ii.remove(); try { c.getMetaData().getTables(null, null, "PROBABLYNOT", new String[] {"TABLE"}); } catch (Exception e) { System.err.println(e); System.err.println(); continue; } finally { c.close(); } Thread.sleep(2000); display(cpds); } System.err.println( "Closing data source, \"forcing\" garbage collection, and sleeping for 5 seconds..."); cpds.close(); System.gc(); System.err.println("Main Thread: Sleeping for five seconds!"); Thread.sleep(5000); // System.gc(); // Thread.sleep(5000); System.err.println("Bye!"); } catch (Exception e) { e.printStackTrace(); } }
private static void usage() { System.err.println("java " + JndiLookupTest.class.getName() + " \\"); System.err.println("\t<dmds_name> <wcpds_name> <wpbds_name>"); System.exit(-1); }