package com.hope.utils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @author newcityman
* @date 2019/11/21 - 20:34
*/
public class ConnectionUtils {
private ThreadLocal<Connection> t1 = new ThreadLocal<Connection>();
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
/**
* 获取当前线程的连接
* @return
*/
public Connection getThreadConnection() {
try {
//1、从当前线程上获取连接
Connection conn = t1.get();
//2、判断当前线程上是否有连接
if (conn == null) {
//3、从数据源中获取连接,并且存入到线程中
conn = dataSource.getConnection();
t1.set(conn);
}
//4、返回当前线程上的连接
return conn;
} catch (SQLException e) {
throw new RuntimeException("创建连接失败");
}
}
/**
* 把线程和连接解绑
*/
public void removeConnection(){
t1.remove();
}
}
package com.hope.utils;
import java.sql.SQLException;
/**
* @author newcityman
* @date 2019/11/21 - 20:52
*/
public class TransactionManager {
private ConnectionUtils connectionUtils;
public void setConnectionUtils(ConnectionUtils connectionUtils) {
this.connectionUtils = connectionUtils;
}
/**
* 开启事务
*/
public void beainTransaction(){
try {
connectionUtils.getThreadConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 提交事务
*/
public void commit(){
try {
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 事务回滚
*/
public void rollback(){
try {
connectionUtils.getThreadConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 开启事务
*/
public void release(){
try {
connectionUtils.getThreadConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//bean.xml
<?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">
<!--配置service-->
<bean id="accountService" class="com.hope.service.impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"/>
<!--注入txManager-->
<property name="txManager" ref="txManager"/>
</bean>
<!--配置dao-->
<bean id="accountDao" class="com.hope.dao.impl.AccountDaoImpl">
<!--注入runner-->
<property name="runner" ref="runner"/>
<!--注入connectionUtils-->
<property name="connectionUtils" ref="connctionUtils"/>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/easy"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<bean name="connctionUtils" class="com.hope.utils.ConnectionUtils">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean name="txManager" class="com.hope.utils.TransactionManager">
<property name="connectionUtils" ref="connctionUtils"/>
</bean>
</beans>