数据库连接池
先要导入Druid(德鲁伊)的jar包
在这个连接中https://developer.aliyun.com/mvn/search找到druid-1.1.17.jar下载并且放入项目中的lib文件下
@Test
public void testConn() throws SQLException {
//1创建数据源
DruidDataSource dataSource=new DruidDataSource();
String driverName="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://127.0.0.1:3306/java_learn?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
String username="root";
String password="xxxxxx";
dataSource.setDriverClassName(driverName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
Connection connection=dataSource.getConnection();
System.out.println(connection);
}
其中将jdbc.properties改为druid.properties,并且里面内容改为
druid.driverName="com.mysql.cj.jdbc.Driver";
druid.url="jdbc:mysql://127.0.0.1:3306/java_learn?serverTimezone=GMT%2B8&&useServerPrepStmts=true&cachePrepStmts=true";
druid.username="root";
druid.password="xxxxxx";
后面测试德鲁伊的一些测试
druid.driverName=com.mysql.cj.jdbc.Driver
druid.url=jdbc:mysql://localhost:3306/java_learn
druid.username=root
druid.password=200625
druid.maxWait=1000
druid.initialSize=10
druid.minIdle=20
druid.maxActive=50
@Test
public void testConn2() throws SQLException, IOException, InterruptedException {
//1创建数据源
DruidDataSource dataSource=new DruidDataSource();
Properties properties=new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties"));
dataSource.configFromPropety(properties);
for (int i = 0; i < 500; i++) {
new Thread(()->{
try{
Connection connection=dataSource.getConnection();
Thread.sleep(new Random().nextInt(1000));
connection.close();
} catch (SQLException | InterruptedException throwables) {
throwables.printStackTrace();
}
}).start();
}
Thread.sleep(20000);
System.out.println(dataSource);
}
Hikari(速度很快)
HikariCP-5.0.1.jar
#下载地址 https://developer.aliyun.com/mvn/search到官网中对slf4j进行下载jaa包
https://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.9/
@Test
public void getConnection() throws SQLException {
// 创建数据源
HikariDataSource dataSource=new HikariDataSource();
// 配置数据参数
dataSource.setUsername("root");
dataSource.setPassword("xxxxxx");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/java_learn");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
System.out.println(dataSource.getConnection());
}
更改数据源
创建一个JdbcUtil的文件如下
public class JdbcUtil {
public final static ThreadLocal<Connection> THREADLOCAL=new ThreadLocal<>();
// 创建数据源
public final static DataSource dataSource;
// 初始化
static {
HikariConfig hikariConfig=new HikariConfig("D:\\java_pro\\jdbc_study\\src\\hikari.properties");
dataSource=new HikariDataSource(hikariConfig);
}
public static Connection getConnection(){
Connection conn=THREADLOCAL.get();
//如果没有连接,创建一个
if(conn==null){
try{
conn=dataSource.getConnection();
if(conn ==null){
throw new RuntimeException("链接获取异常");
}
//创建之后加入threadLocal
THREADLOCAL.set(conn);
}
catch (Exception exception){
exception.printStackTrace();
throw new RuntimeException("链接获取异常!");
}
}
return conn;
}
public static void closeAll(Connection conn,Statement statement,ResultSet resultSet){
if(conn!=null){
try{
conn.close();
JdbcUtil.THREADLOCAL.remove();
}catch (SQLException e){
e.printStackTrace();
}
}
if(statement!=null){
try{
statement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(resultSet!=null){
try{
resultSet.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}