关于C3P0超过最大连接数问题
这几天我在测试C3P0数据库连接池技术时,发现在超过最大连接数后再去申请数据库连接,程序会一直处于等待状态,无任何异常抛出。请问如何获取这类异常信息?
测试代码如下:
import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import java.text.SimpleDateFormat;import com.mchange.v2.c3p0.ComboPooledDataSource;import com.mchange.v2.c3p0.DataSources;public class TestC3P01 { /** * @param args */ public static void main(String[] args) { ComboPooledDataSource cpds = new ComboPooledDataSource(); try { cpds.setDriverClass( "com.mysql.jdbc.Driver" ); } catch (PropertyVetoException e) { e.printStackTrace(); System.exit(1); } cpds.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/test" ); cpds.setUser("hb"); cpds.setPassword("123456"); cpds.setMinPoolSize(5); cpds.setMaxPoolSize(20); cpds.setInitialPoolSize(10); cpds.setAcquireRetryAttempts(1); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Connection[] c = new Connection[20]; for(int i = 0; i < 20; i++) { try { System.out.println(df.format(new java.util.Date())); System.out.println("申请第" + i + "个数据库连接"); c[i] = cpds.getConnection(); System.out.println(df.format(new java.util.Date())); } catch (SQLException e) { System.out.println(df.format(new java.util.Date())); e.printStackTrace(); } } try { System.out.println("超过最大连接数后再次申请数据库连接"); Connection con = cpds.getConnection(); System.out.println(df.format(new java.util.Date())); } catch (Exception e) { System.out.println(df.format(new java.util.Date())); e.printStackTrace(); System.exit(1); } try { DataSources.destroy( cpds ); } catch (SQLException e) { e.printStackTrace(); } }}