DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(DRIVER_CLASS_NAME);
dataSource.setUrl(book.getDbUrl());
dataSource.setUsername(book.getDbUsername());
dataSource.setPassword(book.getDbPassword());
dataSource.setMaxActive(100);
dataSource.setMinIdle(10);
dataSource.setInitialSize(10);
dataSource.setMaxWait(10000); //若没设置 ,默认-1 将一直请求
dataSource.setRemoveAbandonedTimeout(600);
dataSource.setLogAbandoned(true);
dataSource.setBreakAfterAcquireFailure(true); // 在请求失败以后是否中断
dataSource.setConnectionErrorRetryAttempts(0); //请求失败尝试次数
源码:
DruidDataSource
一、请求连接,封装异常
二、抛出异常
三、
1、for循环请求;
2、捕获异常,判断有没有超出请求次数;