当前位置: 首页 > news >正文

Spring2:应用事务+连接池形成的工具类

工具类

package com.qcby.utils;import com.alibaba.druid.pool.DruidDataSource;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;/*** 事务的工具类*/
//事务是通过连接开启的,所以要保证是同一个连接
public class TxUtils {//连接池对象private static DruidDataSource ds = null;//使用ThreadLocal存储当前线程中的connection对象private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();//在静态代码块中创建数据库连接池static {try {//通过代码创建C3P0数据库连接池ds = new DruidDataSource();ds.setDriverClassName("com.mysql.jdbc.Driver");ds.setUrl("jdbc:mysql:///spring_db");ds.setUsername("root");ds.setPassword("root");}catch (Exception e) {throw new ExceptionInInitializerError(e);}}/*** @Method: getConnection* @Description: 丛数据源中获取数据库连接* @Anthor:* @return Connection* @throws SQLException* 保证是相同的连接*/public static Connection getConn() throws SQLException{//从当前线程中获取ConnectionConnection conn = threadLocal.get();if(conn == null){//从数据源中获取数据库连接conn = getDataSource().getConnection();//将conn绑定到当前线程threadLocal.set(conn);}return conn;}/*** @Method: startTransaction* @Description: 开启事务* @Anthor:**/public static void startTransaction(){try {Connection conn = threadLocal.get();if(conn == null) {conn = getConn();//把conn绑定到当前线程上threadLocal.set(conn);}//开启事务conn.setAutoCommit(false);} catch (SQLException e) {throw new RuntimeException(e);}}/*** @Method: rollback* @Description:回滚事务* @Anthor:*/public static void rollback(){try {//从当前线程中获取connectionConnection conn = threadLocal.get();if(conn != null){//回滚事务conn.rollback();}}catch (Exception e) {throw new RuntimeException(e);}}/*** @Method: commit* @Description:提交事务* @Anthor:*/public static void commit(){try {//从当前线程中获取connectionConnection conn = threadLocal.get();if(conn != null){//提交事务conn.commit();}}catch (Exception e) {throw new RuntimeException(e);}}/*** @Method: close* @Description:关闭数据库连接(注意,并不是真的关闭,而是把连接还给数据库连接池)* @Anthor:**/public static void close(){try {Connection conn = threadLocal.get();if(conn != null){conn.close();//解除当前线程上绑定conn,使得每次使用的数据库是一个threadLocal.remove();}} catch (SQLException e) {throw new RuntimeException(e);}}/*** @Method: getDataSource* @Description: 获取数据源* @Anthor:* @return DataSource*/public static DataSource getDataSource(){//从数据源中获取数据库连接return ds;}
}

持久层

package com.qcby.dao;import com.qcby.model.Account;
import com.qcby.utils.TxUtils;
import org.springframework.stereotype.Repository;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;@Repository
public class AccountDaoImpl implements AccountDao {public void save(Account account) {try {//创建连接Connection conn = TxUtils.getConn();//编写sql语句String sql = "update account set money = money - 10 where name = ? ";//预编译sql语句PreparedStatement statement = conn.prepareStatement(sql);//设置值statement.setString(1,account.getName());//执行操作statement.executeUpdate();//关闭资源statement.close();} catch (SQLException e) {e.printStackTrace();}}
}

业务层(service)

package com.qcby.service.impl;import com.qcby.dao.AccountDao;
import com.qcby.model.Account;
import com.qcby.service.AccountService;
import com.qcby.utils.TxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void saveAll(Account account1, Account account2) {try {//开启事务TxUtils.startTransaction();accountDao.save(account1);//int res = 10/0;accountDao.save(account2);TxUtils.commit();}catch (Exception e){e.printStackTrace();//事务回滚TxUtils.rollback();}finally {//关闭资源TxUtils.close();}}
}

相关文章:

  • 【匹配】Hirschberg
  • ​小店推客系统开发SEO全攻略:从技术架构到流量裂变,打造私域增长引擎
  • 代码随想录算法训练营第三十八天打卡
  • 文章记单词 | 第86篇(六级)
  • Guided Filtering相关记录
  • EC800X_DP-DTU-Q600R 系列开发板介绍
  • Gen5 应用实例
  • 主打「反激进」的一汽丰田,靠稳扎稳打的技术实现突围
  • 梁文锋署名,DeepSeek-V3新论文揭秘:低成本大模型训练如何突破算力瓶颈?
  • 【omnet++】omnet++6.0.3中调用python
  • 主流数据库排查与优化速查手册
  • [Unity]AstarPathfindingProject动态烘焙场景
  • 播放进度条小组件
  • C#学习教程(附电子书资料)
  • 413 Payload Too Large 问题定位
  • 【Win32 API】 lstrcpynA()
  • 直接从图片生成 html
  • [Lc] 5.16 One question a day周总结
  • 道路运输企业管理人员考试真题练习
  • 线程和进程
  • 光速晋级!2025年多哈世乒赛孙颖莎4比0战胜对手
  • 竞彩湃|足总杯决赛或有冷门,德甲欧冠资格之争谁笑到最后
  • 科普|认识谵妄:它有哪些表现?患者怎样走出“迷雾”?
  • 俄媒:俄乌代表团抵达谈判会场
  • 外交部:中方对美芬太尼反制仍然有效
  • 落实中美经贸高层会谈重要共识,中方调整对美加征关税措施