MysqlConnPool.java
4.08 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
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
package com.glsx.pool;
import com.glsx.conf.ConfigurationManager;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
public class MysqlConnPool {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static MysqlConnPool instance = null;
public static MysqlConnPool getInstance(String url) {
if (instance == null) {
synchronized (MysqlConnPool.class) {
if (instance == null) {
instance = new MysqlConnPool(url);
}
}
}
return instance;
}
private LinkedList<Connection> datasource = new LinkedList<Connection>();
public MysqlConnPool(String url) {
int datasourceSize = ConfigurationManager.getInteger("mysql.datasource.size");
for (int i = 0; i < datasourceSize; i++) {
try {
Connection connection = DriverManager.getConnection(url);
datasource.add(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public synchronized Connection getConnection() {
while (datasource.size() == 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return datasource.poll();
}
public synchronized void returnConnection(Connection conn) {
datasource.push(conn);
}
public int executeUpdate(String sql, Object[] params) {
int rtn = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
if (params != null && params.length > 0) {
for (int i = 0; i < params.length; i++) {
pstmt.setObject(i+1, params[i]);
}
}
rtn = pstmt.executeUpdate();
conn.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(conn != null) {
datasource.push(conn);
}
}
return rtn;
}
public void executeQuery(String sql, Object[] params, QueryCallback callback) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);
if(params != null && params.length > 0) {
for(int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
}
rs = pstmt.executeQuery();
callback.process(rs);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(conn != null) {
datasource.push(conn);
}
}
}
public int[] executeBatch(String sql, List<Object[]> paramsList) {
int[] rtn = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
if(paramsList != null && paramsList.size() > 0) {
for (Object[] params : paramsList) {
for(int i = 0; i < params.length; i++) {
pstmt.setObject(i + 1, params[i]);
}
pstmt.addBatch();
}
}
rtn = pstmt.executeBatch();
conn.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
datasource.push(conn);
}
}
return rtn;
}
public int size() {
return datasource.size();
}
public static interface QueryCallback {
void process(ResultSet rs) throws Exception;
}
}