数据源基本配置参数
1.c3p0数据源
简介:Hibernate 数据库框架内置数据库连接池,
使用xml文件配置基本数据库连接信息
简化业务逻辑
.XML 配置文件
<c3p0-config>
<!-- 默认配置,只可以出现一次 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/具体databases
?useSSL=false</property>
<property name="user">username</property>
<property name="password">password</property>
<!-- 连接超时设置30秒 -->
<property name="checkoutTimeout">3000</property>
<!-- 30秒检查一次connection的空闲 -->
<property name="idleConnectionTestPeriod">30</property>
<!--初始化的池大小 -->
<property name="initialPoolSize">10</property>
<!-- 最多的一个connection空闲时间 -->
<property name="maxIdleTime">30</property>
<!-- 最多可以有多少个连接connection -->
<property name="maxPoolSize">30</property>
<!-- 最少的池中有几个连接 -->
<property name="minPoolSize">10</property>
<!-- 批处理的语句
-->
<property name="maxStatements">50</property>
<!-- 每次增长几个连接 -->
<property name="acquireIncrement">3</property>
</default-config>
</c3p0-config>
JdbcUtil工具类
package util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* JdbcUtil1功能简述
* 1. 程序预处理驱动问题和连接所需数据
* 2. 获取数据库连接对象
* 3. 关闭数据库操作所使用资源
* 4. 使用c3p0-config.xml数据连接池实现
* @author Mxg
*/
public class JdbcUtil1 {
private static DataSource dataSources = new ComboPooledDataSource();
public static Connection getConnection(){
Connection connection = null;
try {
connection = dataSources.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
/**
* 关闭资源使用方法,需要的参数是AutoCloseable接口实现类对象,
* 不定长参数
*
* @param source AutoCloseable... 不定长参数
*/
public static void close(AutoCloseable... source) {
// 不定长参数在方法中就是一个数组!!!
for (AutoCloseable closeable : source) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2.druid数据源
简介:Druid首先是一个数据库连接池。
Druid是目前最好的数据库连接池,在功能、性能、扩展性方面极为突出;
使用properties配置文件配置基本数据库连接信息
.properties 配置文件
# 文件名 druid.properties 存储在src目录下
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/This is your Mysql database?useSSL=false
username=root
password=Your Mysql password
# 初始化数据库连接池容量
initialSize=10
# 最大容量
maxActive=30
# TimeOut 等待超时时间
maxWait=2000
JdbcUtil工具类
在这里插入代码片package util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* @Author Mxg
* @Description
* @Date
*/
public class Druid {
private static DataSource dataSources = null;
static {
Properties properties = new Properties();
try {
properties.load(Druid.class.getClassLoader().getResourceAsStream("druid.properties"));
dataSources = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSources.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(AutoCloseable... source) {
// 不定长参数在方法中就是一个数组!!!
for (AutoCloseable closeable : source) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}