-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPoolConnect.java
executable file
·143 lines (120 loc) · 3.62 KB
/
PoolConnect.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
package bc.connection;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
public abstract class PoolConnect {
protected void outError(Object information){
//System.out.print("PoolConnect");
//System.out.print(" ERROR ");
//System.out.println(information);
}
protected void outDebug(Object information){
//System.out.print("PoolConnect");
//System.out.print(" DEBUG ");
//System.out.println(information);
}
private ComboPooledDataSource field_cpds=null;
private String userName;
private String password;
private Integer poolSize;
public PoolConnect(String userName, String password, Integer poolSize){
this.userName=userName;
this.password=password;
this.poolSize=poolSize;
}
/** çàêðûòü ïîëíîñòüþ POOL */
public void close(){
try{
DataSources.destroy(field_cpds);
}catch(Exception ex){
}
}
/** ïîëó÷èòü Connection èç POOL */
public Connection getConnection(){
if(field_cpds==null){
outDebug("getConnection: pool is not create ");
try{
create();
outDebug("getConnection: NEW pool has been created, getting Connection");
return field_cpds.getConnection();
}catch(Exception ex){
outError("getConnection: Exception:"+ex.getMessage());
return null;
}
}else{
try{
outDebug("getConnection: pool has been created, getting Connection");
return field_cpds.getConnection();
}catch(Exception ex){
outError("getConnection: Exception:"+ex.getMessage());
return null;
}
}
}
private void create() throws PropertyVetoException{
field_cpds = new ComboPooledDataSource();
field_cpds.setDriverClass(this.geDriverName());
field_cpds.setJdbcUrl(this.getDataBasePath());
field_cpds.setUser(this.getUserName());
field_cpds.setPassword(this.getPassword());
field_cpds.setMinPoolSize(1);
field_cpds.setInitialPoolSize(2);
field_cpds.setAcquireIncrement(1);
field_cpds.setMaxPoolSize(this.getMaxPoolSize());
field_cpds.setMaxConnectionAge(1);
field_cpds.setBreakAfterAcquireFailure(true);
field_cpds.setAcquireRetryAttempts(1);
}
/** çàêðûòü Connection - âåðíóòü Connection â POOL */
public void closeConnection(Connection connection){
try{
connection.close();
}catch(Exception ex){
outError("closeConnection exception:"+ex.getMessage());
}
}
/** ïîëó÷èòü èìÿ êëàññà äðàéâåðà
* "org.firebirdsql.jdbc.FBDriver"
* */
protected abstract String geDriverName();
/** ïîëó÷èòü èìÿ êëàññà äðàéâåðà
* "jdbc:firebirdsql://localhost:3050/D:/temp.gdb?sql_dialect=3"
* */
protected abstract String getDataBasePath();
/** ïîëó÷èòü Èìÿ ïîëüçîâàòåëÿ */
protected String getUserName(){
return this.userName;
}
/** ïîëó÷èòü Ïàðîëü äëÿ ïîëüçîâàòåëÿ */
protected String getPassword(){
return this.password;
}
/** ïîëó÷èòü ìàêñèìàëüíûé ðàçìåð POOL */
protected Integer getMaxPoolSize(){
return this.poolSize;
}
public void finalize(){
this.close();
}
/** reset DataSource*/
public void reset(){
this.field_cpds.resetPoolManager();
}
/** ïðîâåðèòü ïàðîëü íà ýêâèâàëåíòíîñòü */
public boolean isPasswordEquals(String password){
boolean return_value=false;
if((password!=null)&&(this.password!=null)){
return_value=this.password.equals(password);
}
return return_value;
}
/** ïîëó÷èòü êîë-âî òåêóùèõ ñîåäèíåíèé */
public int getConnectionCount(){
try{
return this.field_cpds.getNumConnectionsAllUsers();
}catch(Exception ex){
return -1;
}
}
}