高并发服务优化篇:浅谈数据库连接池
复制//验证思路参考自:https://blog.csdn.net/sunbo94/article/details/79409298
//Connection设置 autoCommit=false
private static final ThreadLocal<Connection> connectionThreadLocal=new ThreadLocal<>();
private static class InnerRunner implements Runnable{
@Override
public void run() {
//其他代码省略...
String insertSql="insert into user(id,name) value("+RunnerIndex+","+RunnerIndex+")";
statement=connectionThreadLocal.get().createStatement();
statement.executeUpdate(insertSql);
System.out.println(RunnerIndex+" is running");
//让特定的线程执行回滚,用来验证事务之间的影响
if (RunnerIndex==3){
//模拟异常时耗时增加
Thread.sleep(100);
//从threadlocal里拿连接对象
connectionThreadLocal.get().rollback();
System.out.println("3 rollback");
}else{
//从threadlocal里拿连接对象
connectionThreadLocal.get().commit();
System.out.println(RunnerIndex +" commit");
}
}
}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.
THE END