JavaWeb之ThreadLocal的使用
ThreadLocal的作用:它可以解决多线程的数据安全问题。
ThreadLocal可以给当前线程关联一个数据(可以是普通变量、对象、数组、集合)
ThreadLocal的特点:
1.ThreadLocal可以为当前线程关联一个数据(它可以像Map一样存取数据,key为当前线程)
2.每一个ThreadLocal对象,只能为当前线程关联一个数据,如果要为当前线程关联多个数据,就需要使用多个ThreadLocal对象实例。
3.每个ThreadLocal对象实例定义的时候,一般都是static类型。
4.ThreadLocal中保存数据,在线程销毁后,会由JVM虚拟机自动释放。
在一个线程的执行过程中,通过threadLocal的set方法将当前线程生成的随机数保存,之后通过threadLocal.get()方法就能准确的获取当前线程存入的随机数,而不会读取其他线程保存的数据,从而实现线程中数据的安全性。
package threadlocal;
public class ThreadLocalTest {
public static ThreadLocal<Object> threadLocal = new ThreadLocal<>();
public static class Task implements Runnable {
@Override
public void run() {
//在run方法中,随机生成一个变量(线程要关联的数据),如何44然后以当前线程名为Key保存到map中
//获取0-1000的随机整数
Integer i = (int) (Math.random() * 1000);
//获取当前线程名
String name = Thread.currentThread().getName();
System.out.println("线程[" + name + "]生成的随机数为:" + i);
threadLocal.set(i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//在run方法结束之前,以当前线程名获取数据并打印。查看是否可以取出操作
Object o = threadLocal.get();
System.out.println("在线程[" + name + "]快结束时取出关联的数据是:" + o);
}
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
new Thread(new Task()).start();
}
}
}
}
运行结果如下:
线程[Thread-2]生成的随机数为:823
线程[Thread-0]生成的随机数为:55
线程[Thread-1]生成的随机数为:588
在线程[Thread-1]快结束时取出关联的数据是:588
在线程[Thread-0]快结束时取出关联的数据是:55
在线程[Thread-2]快结束时取出关联的数据是:823
使用ThreadLocal保证数据库数据的原子性
使用场景:多次对数据库进行更新,保证所有的更新操作要么都执行,要么都不执行。
事务原子性的图例:
使用ThreadLocal保证数据原子性的条件:
使用ThreadLocal实现JDBCUtils:(基于Druid数据库连接池)
public class JdbcUtils {
private static DruidDataSource dataSource;
private static ThreadLocal<Connection> conns = new ThreadLocal<>();
static {
try {
Properties properties = new Properties();
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(in);
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return conn
*/
public static Connection getConnection() {
//从ThreadLocal中获取连接
Connection conn = conns.get();
if (conn == null) {
try {
//从druid连接池中获取连接
conn = dataSource.getConnection();
//将连接保存到ThreadLocal对象中,供后面的JDBC操作使用
conns.set(conn);
//设置数据库事务为手动管理
conn.setAutoCommit(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return conn;
}
/**
* 提交事务并关闭释放连接
* @param
*/
public static void commitAndClose(){
Connection conn = conns.get();
if(conn != null){
//conn不为空,说明之前操作过事务
try {
//提交事务,关闭连接
conn.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//一定要执行remove()操作,否则会出错。(因为Tomcat服务器底层使用了线程池技术)
conns.remove();
}
/**
* 回滚事务
* @param
*/
public static void rollBackAndClose(){
Connection conn = conns.get();
if(conn != null){
//conn不为空,说明之前操作过事务
try {
//提交事务,关闭连接
conn.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//一定要执行remove()操作,否则会出错。(因为Tomcat服务器底层使用了线程池技术)
conns.remove();
}
/**
* 关闭连接
* @param conn
*/
public static void close(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
使用ThreadLocal的BaseDao:
去除conn.close() :保证整个事务都使用同一个连接
catch中必须抛出异常,使事务在执行过程中通过是否catch到异常来决定回滚/提交,没用异常则提交,从而保证两表同时修改。
public abstract class BaseDao {
private QueryRunner queryRunner = new QueryRunner();
public int update(String sql,Object ... args){
Connection conn = JdbcUtils.getConnection();
try {
return queryRunner.update(conn,sql,args);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public <T> T queryForOne(Class<T> type,String sql,Object...args){
Connection conn = JdbcUtils.getConnection();
try {
T query = queryRunner.query(conn, sql, new BeanHandler<T>(type), args);
return query;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public <T> List<T> queryForList(Class<T> type, String sql, Object...args){
Connection conn=JdbcUtils.getConnection();
try {
return queryRunner.query(conn, sql, new BeanListHandler<T>(type), args);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public Object queryForSingleValue(String sql,Object...args){
Connection conn = JdbcUtils.getConnection();
try {
return queryRunner.query(conn,sql, new ScalarHandler(),args);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
ThreadLocal提交和回滚事务事例
createOrder同时修改订单表和订单商品表,如果出现异常则立即回滚。
try {
orderId = orderService.createOrder(cart, userId);
JdbcUtils.commitAndClose();//提交事务
} catch (Exception e) {
JdbcUtils.rollBackAndClose();//回滚事务
e.printStackTrace();
}