文章目录
- properties文件
- 连接池
- service层实现类
- dao层实现类
- dao层实现类
连接池类:
创建线程池静态常量,用于放连接。
创建Properties静态常量,用于解析properties文件
静态代码块中,解析properties文件,将解析结果用于创建连接池
连接方法:用线程获取连接,若没有,从连接池中拿一个连接,放到线程中去
释放连接方法:将线程变量清除,去除事务,连接放到连接池中
service层实现类
从连接池中获取一个连接,这个连接是在线程中
设置事务(自动提交为false)
获取两个dao,两个dao都是操作同一个连接
提交事务
释放连接
dao层减钱、加钱
properties文件
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=root
initialSize=10
maxActive=20
minIdle=5
maxWait=5000
连接池
package com.aistart.tech.utils;import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;public class JdbcPoolUtil {private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();private static final Properties info = new Properties();private static DataSource dataSource = null;
// private static Connection connection = null;static {try {info.load(JdbcPoolUtil.class.getClassLoader().getResourceAsStream("com/aistart/tech/config/druid.properties"));} catch (IOException e) {throw new RuntimeException(e);}//数据库连接池应该也同时被创建try {dataSource = DruidDataSourceFactory.createDataSource(info);} catch (Exception e) {throw new RuntimeException(e);}}public static Connection getConnection() throws SQLException {Connection connection = threadLocal.get();if (connection==null){connection = dataSource.getConnection();threadLocal.set(connection);}return connection;}public static void freeConnection(){Connection connection = threadLocal.get();if (connection!=null){try {//把线程变量清除threadLocal.remove();//恢复原来的状态connection.setAutoCommit(true);connection.close();} catch (SQLException e) {throw new RuntimeException(e);}}}}
service层实现类
package com.aistart.tech.servcice.impl;import com.aistart.tech.dao.IAccuntDao;
import com.aistart.tech.dao.impl.AccuntDaoImpl;
import com.aistart.tech.entity.Accunt;
import com.aistart.tech.servcice.AccountService;
import com.aistart.tech.utils.JdbcPoolUtil;import java.sql.Connection;
import java.sql.SQLException;public class AccountServiceImpl implements AccountService {IAccuntDao accuntDao = new AccuntDaoImpl();@Overridepublic int getBalance(int id) {return 0;}@Overridepublic boolean transferMoney(Accunt accunt1, Accunt accunt2,int money) {Connection connection = null;try {//从数据库连接池拿出来一个连接connection = JdbcPoolUtil.getConnection();connection.setAutoCommit(false);accuntDao.subMoney(accunt1.getId(),money);accuntDao.addMoney(accunt2.getId(),money);connection.commit();System.out.println("service的conn"+ connection.getAutoCommit());} catch (Exception e) {try {connection.rollback();} catch (SQLException ex) {throw new RuntimeException(ex);}throw new RuntimeException(e);}finally {JdbcPoolUtil.freeConnection();}return false;}
}
dao层实现类
package com.aistart.tech.dao.impl;import com.aistart.tech.dao.IAccuntDao;
import com.aistart.tech.utils.JdbcPoolUtil;import java.sql.Connection;
import java.sql.SQLException;public class AccuntDaoImpl implements IAccuntDao {@Overridepublic int subMoney(int id, int money) {Connection connection = null;if (connection == null) {try {connection = JdbcPoolUtil.getConnection();System.out.println("这是dao层的connection"+connection.getAutoCommit());} catch (SQLException e) {throw new RuntimeException(e);}}return 0;}@Overridepublic int addMoney(int id, int money) {return 0;}
}
dao层实现类
package com.aistart.tech.dao.impl;import com.aistart.tech.dao.IAccuntDao;
import com.aistart.tech.utils.JdbcPoolUtil;import java.sql.Connection;
import java.sql.SQLException;public class AccuntDaoImpl implements IAccuntDao {@Overridepublic int subMoney(int id, int money) {Connection connection = null;if (connection == null) {try {connection = JdbcPoolUtil.getConnection();System.out.println("这是dao层的connection"+connection.getAutoCommit());} catch (SQLException e) {throw new RuntimeException(e);}}return 0;}@Overridepublic int addMoney(int id, int money) {return 0;}
}