1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package goldengate.common.database;
21
22 import goldengate.common.logging.GgInternalLogger;
23 import goldengate.common.logging.GgInternalLoggerFactory;
24
25 import java.sql.Connection;
26 import java.sql.SQLException;
27 import java.util.concurrent.ConcurrentHashMap;
28
29 import goldengate.common.database.exception.GoldenGateDatabaseNoConnectionException;
30 import goldengate.common.database.exception.GoldenGateDatabaseSqlException;
31 import goldengate.common.database.model.DbType;
32 import goldengate.common.database.model.DbModelFactory;
33
34
35
36
37
38
39
40 public class DbAdmin {
41
42
43
44 private static final GgInternalLogger logger = GgInternalLoggerFactory
45 .getLogger(DbAdmin.class);
46
47 public static int RETRYNB = 3;
48
49 public static long WAITFORNETOP = 100;
50
51
52
53
54 public DbType typeDriver;
55
56
57
58
59 private String server = null;
60
61
62
63
64 private String user = null;
65
66
67
68
69 private String passwd = null;
70
71
72
73
74 public boolean isConnected = false;
75
76
77
78
79 public boolean isReadOnly = false;
80
81
82
83
84
85 public boolean isMultipleDBAccess = false;
86
87
88
89
90 public DbSession session = null;
91
92
93
94
95
96
97 public void validConnection() throws GoldenGateDatabaseNoConnectionException {
98 try {
99 DbModelFactory.dbModel.validConnection(session);
100 } catch (GoldenGateDatabaseNoConnectionException e) {
101 session.isDisconnected = true;
102 isConnected = false;
103 throw e;
104 }
105 session.isDisconnected = false;
106 isConnected = true;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public DbAdmin(DbType driver, String server, String user, String passwd)
128 throws GoldenGateDatabaseNoConnectionException {
129 this.server = server;
130 this.user = user;
131 this.passwd = passwd;
132 this.typeDriver = driver;
133 if (typeDriver == null) {
134 logger.error("Cannot find TypeDriver:" + driver.name());
135 throw new GoldenGateDatabaseNoConnectionException(
136 "Cannot find database drive:" + driver.name());
137 }
138 session = new DbSession(this.server, this.user, this.passwd, false);
139 session.admin = this;
140 isReadOnly = false;
141 validConnection();
142 session.useConnection();
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 public DbAdmin(DbType driver, String server, String user, String passwd,
166 boolean write) throws GoldenGateDatabaseNoConnectionException {
167 this.server = server;
168 this.user = user;
169 this.passwd = passwd;
170 this.typeDriver = driver;
171 if (typeDriver == null) {
172 logger.error("Cannot find TypeDriver");
173 throw new GoldenGateDatabaseNoConnectionException(
174 "Cannot find database driver");
175 }
176 if (write) {
177 for (int i = 0; i < RETRYNB; i ++) {
178 try {
179 session = new DbSession(this.server, this.user,
180 this.passwd, false);
181 } catch (GoldenGateDatabaseNoConnectionException e) {
182 logger.warn("Attempt of connection in error: " + i);
183 continue;
184 }
185 isReadOnly = false;
186 session.admin = this;
187 validConnection();
188 session.useConnection();
189
190 return;
191 }
192 } else {
193 for (int i = 0; i < RETRYNB; i ++) {
194 try {
195 session = new DbSession(this.server, this.user,
196 this.passwd, true);
197 } catch (GoldenGateDatabaseNoConnectionException e) {
198 logger.warn("Attempt of connection in error: " + i);
199 continue;
200 }
201 isReadOnly = true;
202 session.admin = this;
203 validConnection();
204 session.useConnection();
205
206 return;
207 }
208 }
209 session = null;
210 isConnected = false;
211 logger.error("Cannot connect to Database!");
212 throw new GoldenGateDatabaseNoConnectionException(
213 "Cannot connect to database");
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 public DbAdmin(Connection conn, boolean isread)
235 throws GoldenGateDatabaseNoConnectionException {
236 server = null;
237 if (conn == null) {
238 session = null;
239 isConnected = false;
240 logger.error("Cannot Get a Connection from Datasource");
241 throw new GoldenGateDatabaseNoConnectionException(
242 "Cannot Get a Connection from Datasource");
243 }
244 session = new DbSession(conn, isread);
245 session.admin = this;
246 isReadOnly = isread;
247 isConnected = true;
248 validConnection();
249 session.useConnection();
250 }
251
252
253
254
255 public DbAdmin() {
256
257 DbModelFactory.classLoaded = true;
258 isConnected = false;
259 }
260
261
262
263
264
265
266 public void close() {
267 if (session != null) {
268 session.endUseConnection();
269
270 session.disconnect();
271 session = null;
272 }
273 isConnected = false;
274 }
275
276
277
278
279
280
281
282
283 public void commit() throws GoldenGateDatabaseSqlException,
284 GoldenGateDatabaseNoConnectionException {
285 if (session != null) {
286 session.commit();
287 }
288 }
289
290
291
292
293 public String getServer() {
294 return server;
295 }
296
297
298
299
300 public String getUser() {
301 return user;
302 }
303
304
305
306
307 public String getPasswd() {
308 return passwd;
309 }
310
311 @Override
312 public String toString() {
313 return "Admin: "+typeDriver.name()+":"+server+":"+user+":"+(passwd.length());
314 }
315
316
317
318
319 private static ConcurrentHashMap<Long, DbSession> listConnection = new ConcurrentHashMap<Long, DbSession>();
320
321
322
323
324 public static int nbHttpSession = 0;
325
326
327
328
329
330
331
332 public static void addConnection(long id, DbSession session) {
333 listConnection.put(Long.valueOf(id), session);
334 }
335
336
337
338
339
340
341
342 public static void removeConnection(long id) {
343 listConnection.remove(Long.valueOf(id));
344 }
345
346
347
348
349
350 public static int getNbConnection() {
351 return listConnection.size() - 1;
352 }
353
354
355
356
357 public static void closeAllConnection() {
358 for (DbSession session: listConnection.values()) {
359 try {
360 session.conn.close();
361 } catch (SQLException e) {
362 }
363 }
364 listConnection.clear();
365 if (DbModelFactory.dbModel != null) {
366 DbModelFactory.dbModel.releaseResources();
367 }
368 }
369
370
371
372
373 public static void checkAllConnections() {
374 for (DbSession session: listConnection.values()) {
375 try {
376 session.checkConnection();
377 } catch (GoldenGateDatabaseNoConnectionException e) {
378 logger.error("Database Connection cannot be reinitialized");
379 }
380 }
381 }
382 }