-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnManager.java
283 lines (259 loc) · 7 KB
/
ConnManager.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.banck.poup.comment.utils;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ConnManager {
private static Logger logger = Logger.getLogger(ConnManager.class);
private static final ThreadLocal<Connection> threadLocal;
private static Properties prop = new Properties();
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
static {
try {
prop.load(ConnManager.class.getClassLoader().getResourceAsStream(
"jdbc.properties"));
} catch (IOException e) {
logger.error("属性文件读取错误", e);
}
dataSource.setUser(prop.getProperty("jdbc.username"));
//增加密码安全机制
try {
String password = new DesUtils("ccbpccs").decrypt(prop.getProperty("jdbc.password"));
dataSource.setPassword(password);
} catch (Exception e){
logger.error("获取密码错误", e);
}
dataSource.setJdbcUrl(prop.getProperty("jdbc.url"));
try {
dataSource.setDriverClass(prop.getProperty("jdbc.driverclass"));
} catch (PropertyVetoException e) {
logger.error("连接池构造异常", e);
throw new IllegalStateException(e);
}
dataSource.setInitialPoolSize(Integer.parseInt(prop
.getProperty("jdbc.initialPoolSize")));
dataSource.setMinPoolSize(Integer.parseInt(prop
.getProperty("jdbc.minPoolSize")));
dataSource.setMaxPoolSize(Integer.parseInt(prop
.getProperty("jdbc.maxPoolSize")));
dataSource.setMaxStatements(Integer.parseInt(prop
.getProperty("jdbc.maxStatements")));
dataSource.setMaxIdleTime(Integer.parseInt(prop
.getProperty("jdbc.maxidletime")));
threadLocal = new ThreadLocal<Connection>();
}
public static Connection getConn() {
Connection conn = threadLocal.get();
try {
if (conn == null || conn.isClosed()) {
conn = dataSource.getConnection();
threadLocal.set(conn);
}
} catch (SQLException e) {
logger.error("获取连接异常", e);
throw new IllegalStateException(e);
}
return conn;
}
public static void closeConn() {
Connection conn = threadLocal.get();
threadLocal.set(null);
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
logger.error("关闭连接异常", e);
// throw new IllegalStateException(e);
}
}
public static void beginTransaction() {
try {
getConn().setAutoCommit(false);
} catch (SQLException e) {
logger.error("开始事务异常", e);
throw new IllegalStateException(e);
}
}
public static void commit() {
try {
getConn().commit();
getConn().setAutoCommit(true);
} catch (SQLException e) {
logger.error("提交事务异常", e);
throw new IllegalStateException(e);
}
}
public static void rollback() {
try {
getConn().rollback();
getConn().setAutoCommit(true);
} catch (SQLException e) {
logger.error("事务回滚异常", e);
throw new IllegalStateException(e);
}
}
// public static List<?> executeQuery(String sql, AbstractDAO<?> dao) {
// PreparedStatement pstmt = null;
// ResultSet rs = null;
// try {
// pstmt = getConn().prepareStatement(sql);
// rs = pstmt.executeQuery();
// List<?> objList = dao.mapping(rs);
// return objList;
// } catch (SQLException e) {
// logger.error("查询异常,查询语句[" + sql + "]", e);
// return null;
// } finally {
// try {
// if (rs != null)
// rs.close();
// if (pstmt != null)
// pstmt.close();
// } catch (Exception e) {
// logger.error("statement关闭异常", e);
// }
// }
// }
public static List<?> executeQuery(String sql) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = getConn().createStatement();
rs = stmt.executeQuery(sql);
List<Map<String,Object>> objList = getMap(rs);
return objList;
} catch (SQLException e) {
logger.error("查询异常,查询语句[" + sql + "]", e);
return null;
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
} catch (Exception e) {
logger.error("statement关闭异常", e);
}
}
}
private static List<Map<String, Object>> getMap(ResultSet rs) {
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
try {
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
Map<String,Object> rowData = new HashMap<String,Object>();
for (int i = 1; i <= columnCount; i++) {
rowData.put(rsmd.getColumnName(i), rs.getObject(i));
}
result.add(rowData);
}
} catch (SQLException e) {
logger.error(e.getStackTrace());
e.printStackTrace();
}
return result;
}
public static boolean execute(String sql) {
Statement stmt = null;
try {
stmt = getConn().createStatement();
return stmt.execute(sql);
} catch (SQLException e) {
logger.error("SQL异常,SQL语句[" + sql + "]", e);
return false;
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
logger.error("statement关闭异常", e);
}
}
}
}
public static int executeUpdate(String sql) {
Statement stmt = null;
try {
stmt = getConn().createStatement();
return stmt.executeUpdate(sql);
} catch (SQLException e) {
logger.error("SQL异常,SQL语句[" + sql + "]", e);
return 0;
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
logger.error("statement关闭异常", e);
}
}
}
}
public static boolean call(String classname) {
CallableStatement call = null;
try {
call = getConn().prepareCall("{CALL " + classname + "}");
return call.execute();
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
if (call != null) {
try {
call.close();
} catch (SQLException e) {
logger.error("CallableStatement关闭异常", e);
}
}
}
}
/**
* 释放资源
*
* @param rs
* @param ps
* @param conn
*/
public static void release(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
rs = null;
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ps = null;
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}