forked from bage2014/study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThreadLocal.java
More file actions
66 lines (51 loc) · 1.63 KB
/
Copy pathMyThreadLocal.java
File metadata and controls
66 lines (51 loc) · 1.63 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.bage.study.java.multhread;
import java.sql.Connection;
import java.sql.SQLException;
/**
* ThreadLocal 使用类
* ThreadLocal 是线程的局部变量, 是每一个线程所单独持有的,其他线程不能对其进行访问<br>
* 最常见的ThreadLocal使用场景为 用来解决 数据库连接、Session管理等。<br>
* @author bage
*
*/
public class MyThreadLocal {
public static void main(String[] args) {
// 存在多线程下,Connection出现报错问题
Connection conn=DBUtil.getConnection();
service(conn);
}
private static void service(Connection conn) {
try {
//获取链接
conn.setAutoCommit(false);//关闭自动提交事务(开启事务);
//执行操作,更新产品,插入日志
// TODO
//最后提交事务
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭连接
DBUtil.closeConnection();
}
}
}
class DBUtil{
// 避免多线程下,线程1还在操作,但是线程2进行了commit后出现报错情况
// 使用 ThreadLocal来存每个线程下的自己的 Connection
static ThreadLocal<Connection> connectionHolder = new ThreadLocal<Connection>();
/**
* 存在多线程下,线程1还在操作,但是线程2进行了commit后出现报错
* @return
*/
public static Connection getConnection() {
return connectionHolder.get();
}
public static void closeConnection() {
try {
connectionHolder.get().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}