ConnectionOraclePool.java
3.94 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
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
package com.sitech.database;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import com.sitech.util.Formater;
/**
* @author zhangdj Email:Mirror_Jean@qq.com
* @version , Oct 22, 2010
*/
public class ConnectionOraclePool {
private static int iDrivertype = 0;
private static String sUserName;
private static String sPassword;
private static String sUrl;
private static String sClassforName;
private static boolean waitingcloseconnection = false;
private static ConnectionOraclePool instance = new ConnectionOraclePool();
static private java.sql.Connection conn = null;
static private java.util.Date connDate = null;
static private int connCount = 0;
static private int currconnCount = 0;
private boolean bCommitstate;
private ConnectionOraclePool() {
}
public static ConnectionOraclePool getInstance() {
return instance;
}
public java.sql.Connection Connection() throws SQLException {
return conn;
}
public synchronized void increase(int i) {
connCount++;
currconnCount = currconnCount + i;
}
synchronized public java.sql.Connection createConnection()
throws SQLException {
if (waitingcloseconnection) {
try {
while (waitingcloseconnection) {
Thread.sleep(1);
}
} catch (Exception e) {
}
}
increase(1);
if (conn != null && !conn.isClosed())
return conn;
try {
Class.forName(sClassforName).newInstance();
conn = DriverManager.getConnection(sUrl, sUserName, sPassword);
connDate = new Date();
connCount = 0;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
synchronized public void closeConn() throws SQLException {
Date preRefresh = Formater.computeDate(new Date(), 'f', -10);// l��10������Ч
if (preRefresh.after(connDate) || connCount > 500) {
if (conn != null) {
if (currconnCount > 1) {
waitingcloseconnection = true;
try {
int i = 0;
while (currconnCount > 1 && i < 10) {
Thread.sleep(80); // �ȴ�50���룬��ǰ����ִ����ϣ���Լ2���ɱ������������κ������Ҫ��2������ɡ�
}
} catch (Exception e) {
e.printStackTrace();
}
}
conn.close();
conn = null;
increase(-1);
waitingcloseconnection = false;
}
}
}
protected void finalize() throws Throwable {
if (conn != null) {
conn.close();
conn = null;
}
}
public java.sql.Statement createStatement() throws SQLException {
if (conn != null)
return conn.createStatement();
else
return null;
}
public java.sql.Statement createScrollStatement() throws SQLException {
if (conn != null)
if (iDrivertype == 0)
return conn.createStatement();
else
return conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
else
return null;
}
public java.sql.PreparedStatement createPreparedStatement(String sql)
throws SQLException {
if (conn != null)
return conn.prepareStatement(sql);
else
return null;
}
/* execute Prepared Statement */
public void executePreparedStmt(java.sql.PreparedStatement pstmt)
throws SQLException {
if (conn != null) {
pstmt.executeUpdate();
} else
return;
}
/* execute Prepared Statement */
public java.sql.CallableStatement createPreparedCall(String sql)
throws SQLException {
if (conn != null) {
return conn.prepareCall(sql);
} else
return null;
}
public void beginTransaction() throws SQLException {
if (conn != null) {
bCommitstate = conn.getAutoCommit();
conn.setAutoCommit(false);
}
}
public void endTransaction() throws SQLException {
if (conn != null) {
if (bCommitstate != conn.getAutoCommit())
conn.setAutoCommit(bCommitstate);
}
}
public void commit() throws SQLException {
if (conn != null)
conn.commit();
}
public void rollback() throws SQLException {
if (conn != null)
conn.rollback();
}
}