下面将通过一个简单示例演示 BoneCP 和 Spring 集成,通过 JdbcTemplate 从 MySQL 5.7.* 中获取数据库时间戳。
下面是示例的 Maven 依赖,直接将其复制到 pom.xml 即可。内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hxstrive.jdbc_pool</groupId>
<artifactId>bonecp_spring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.jolbox/bonecp -->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<!-- Spring 上下文依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
<!-- JdbcTemplate 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.22</version>
</dependency>
<!-- 数据库驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
</project>在项目的 resources 目录下创建一个 application-context.xml 配置文件,用来定义 Spring 的 Bean,分别配置数据源 com.jolbox.bonecp.BoneCPDataSource 和 org.springframework.jdbc.core.JdbcTemplate,后续代码我们将使用 JdbcTemplate 来操作数据库。配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置数据源 -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<!-- 数据库驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<!-- 相应驱动的jdbcUrl,你懂的 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useSSL=false" />
<!-- 数据库的用户名 -->
<property name="username" value="root" />
<!-- 数据库的密码 -->
<property name="password" value="aaaaaa" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="200" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="5" />
<!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定-->
<property name="partitionCount" value="4" />
<!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
<property name="acquireIncrement" value="2" />
<!-- 缓存prepared statements的大小,默认值:0 -->
<property name="statementsCacheSize" value="10" />
</bean>
<!-- 配置 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 引用数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
</beans>源 com.jolbox.bonecp.BoneCPDataSource 还支持一些其他的配置选项,它们包括:
connectionTestStatement:在做keep-alive的时候的SQL语句。
statementsCachedPerConnection:No of statements that can be cached per connection,反正源码中不推荐使用,就别用了.
initSQL: 在每次到数据库取连接的时候执行的SQL语句,只执行一次。
closeConnectionWatch:如果设置为true,则会增加一个线程监控关闭连接时的情况,如果关闭时出现异常,则打出错误日志,主要用于debug。上线后记得关掉。
logStatementsEnabled:如果设置为true,就会打印执行的SQL语句,如果你用了其他能打印SQL语句的框架,那就不必了。
acquireRetryDelay:在获取连接失败后,第二次参试前的延迟时间,默认为7000毫秒。
acquireRetryAttempts:在获取连接失败后的重试次数,默认为5次。
lazyInit:如果设置为true,那么连接池不会自动创建最小连接数的链接,而是保持为空,直到有需求要获取连接。
transactionRecoveryEnabled:如果设置为true,则会保存该链接上的所有活动,以备下次重试的时候使用,这里指的活动是数据库操作。
connectionHookClassName:Connection hook class name.没看懂…
poolName:上面特性中说到的自定义连接池名称。
disableJMX:控制JMX的支持开关。
connectionTimeout:获取连接的时候最大的等待时间,默认值为:Long.MAX_VALUE
万事俱备只欠东风,下面我们将给出一个简单的客户端来使用 JdbcTemplate,代码如下:
package com.hxstrive.jdbc_pool.bonecp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.Timestamp;
/**
* 配置 BoneCP 为 JdbcTemplate 的数据源,利用 JdbcTemplate 获取数据库当前时间戳。
* @author hxstrive.com
*/
public class BoneCpHello {
public static void main(String[] args) {
// 1.从 classpath 中加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"application-context.xml");
// 2.获取 JdbcTemplate 实例 Bean
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
System.out.println("jdbcTemplate = " + jdbcTemplate);
// 3.获取数据库当前的时间戳
Timestamp ts = jdbcTemplate.queryForObject("select CURRENT_TIMESTAMP() as ct", Timestamp.class);
System.out.println(ts);
}
}运行客户端,输出如下:
jdbcTemplate = org.springframework.jdbc.core.JdbcTemplate@5c072e3f
2023-07-03 12:27:29.0