-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDBCUtils.java
151 lines (126 loc) · 3.84 KB
/
JDBCUtils.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package pub.liyf.common;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JDBCUtils {
//使用配置文件的默认配置
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
//事务专用连接
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
/*
* 获得Connection对象
*/
public static Connection getConnection() throws SQLException {
//当connection不等于null说明已经调用过beginTransaction()
Connection connection = threadLocal.get();
if(connection != null) {
return connection;
} else {
return dataSource.getConnection();
}
}
/*
* 释放资源
*/
public static void close(Connection connection,Statement statement) {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(Connection connection,Statement statement,ResultSet resultSet) {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static DataSource getDataSource() {
return dataSource;
}
/*
* 开启事务
* 1.获取Connection对象
* 2.设置setAutoCommit(false)
* 需要保证与DAO中使用的是同一个Connection对象
*/
public static void beginTransaction() throws SQLException {
/*
* 1.给connection赋值
* 2.设置setAutoCommit(false)
*/
Connection connection = threadLocal.get();
if (connection != null) {
throw new SQLException("已经开启事务!");
}
connection = getConnection();
connection.setAutoCommit(false);
threadLocal.set(connection);
}
/*
* 提交事务
* 1.获取beginTransaction提供的Connection对象
* 2.调用Connection#commit
*/
public static void commitTransaction() throws SQLException {
Connection connection = threadLocal.get(); //获取当前线程的专用连接
if (connection == null) {
throw new SQLException("还没有开启事务,无法提交!");
}
connection.commit();
connection.close();
threadLocal.remove();
}
public static void rollbackTransaction() throws SQLException {
Connection connection = threadLocal.get();
if (connection == null) {
throw new SQLException("还没有开启事务,无法回滚!");
}
connection.rollback();
connection.close();
threadLocal.remove();
}
public static void releaseConnection(Connection connection) throws SQLException {
/*
* 如果是事务zhuanyongConnection就不关闭
* 如果不是事务专用,那么就要关闭
*/
Connection con = threadLocal.get();
if(con == null) { //说明没有开启事务
connection.close();
}
if(connection != con) { //有事务,但需要判断参数连接是否与this.connection相等
connection.close();
}
}
}