KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > dbm > JDBCDataSource


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JDBCDataSource.java,v 1.4 2004/10/27 12:35:02 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.dbm;
27
28 // JOnAS imports
29
import org.objectweb.jonas.management.ReconfiguredProp;
30 import org.objectweb.jonas.management.j2eemanagement.J2EEManagedObject;
31
32 /**
33  * MBean class for JDBCDataSource Management
34  * This MBean manages a connection manager and its associated pool. This couple
35  * represents the JOnAS data source implementation
36  *
37  * @author Eric Hardesty JSR 77 (J2EE Management Standard)
38  * @author Adriana Danes add support for persistent reconfiguration
39  */

40 public class JDBCDataSource extends J2EEManagedObject {
41
42     /**
43      * OBJECT_NAME of the associated JDBCDriver MBean
44      */

45     private String JavaDoc jdbcDriver = null;
46
47     /**
48      * Associated connection manager
49      */

50     private ConnectionManager cm = null;
51
52     /**
53      * Pool associated to the connection manager
54      */

55     private Pool pool = null;
56
57     /**
58      * The current data source name
59      */

60     private String JavaDoc datasourceName = null;
61
62     /**
63      * Value used as sequence number by reconfiguration notifications
64      */

65     private long sequenceNumber = 0;
66
67     /**
68      * @param objectName This MBean's OBJECT_NAME
69      * @param cm Associated ConnectionManager reference
70      */

71     public JDBCDataSource(String JavaDoc objectName, ConnectionManager cm) {
72         super(objectName);
73         this.cm = cm;
74         this.pool = cm.getPool();
75         this.datasourceName = cm.getDatasourceName();
76     }
77
78     // JDBC DataSource Configuration
79

80     /**
81      * @return The OBJECT_NAME of the associated JDBCDriver MBean
82      */

83     public String JavaDoc getJdbcDriver() {
84         return jdbcDriver;
85     }
86
87     /**
88      * @param jdbcDriverObjectName OBJECT_NAME of the associated JDBCDriver MBean
89      */

90     public void setJdbcDriver(String JavaDoc jdbcDriverObjectName) {
91         jdbcDriver = jdbcDriverObjectName;
92     }
93
94     /**
95      * Return the current data source name. Returns the local attribute value (optimize
96      * call) - this value can't be changed, moreover, its used by private methods.
97      * @return The current data source name
98      */

99     public String JavaDoc getName() {
100         return datasourceName;
101     }
102
103     /**
104      * @return The JNDI name
105      */

106     public String JavaDoc getJndiName() {
107         return cm.getDSName();
108     }
109
110     /**
111      * @return Description of this data source
112      */

113     public String JavaDoc getDescription() {
114         return cm.getDataSourceDescription();
115     }
116
117     /**
118      * @return The JDBC URL for the database
119      */

120     public String JavaDoc getUrl() {
121         return cm.getUrl();
122     }
123
124     /**
125      * @return The user name for connection to the database
126      */

127     public String JavaDoc getUserName() {
128         return cm.getUserName();
129     }
130
131     /**
132      * @return The password for connection to the database
133      */

134     public String JavaDoc getUserPassword() {
135         return cm.getPassword();
136     }
137
138     /**
139      * @return The mapper JORM for the database
140      */

141     public String JavaDoc getMapperName() {
142         return cm.getMapperName();
143     }
144
145     // JDBC Connection Pool Configuration
146

147     /**
148      * @return JDBC connection checking level
149      */

150     public Integer JavaDoc getJdbcConnCheckLevel() {
151         return new Integer JavaDoc(pool.getCheckLevel());
152     }
153     /**
154      * Sets the JDBC connection checking level
155      * @param level connection level
156      */

157     public void setJdbcConnCheckLevel(Integer JavaDoc level) {
158         pool.setCheckLevel(level.intValue());
159         String JavaDoc propName = DataBaseServiceImpl.CONNCHECKLEVEL;
160         ReconfiguredProp prop = new ReconfiguredProp(propName, level.toString());
161         // Send a reconfiguration notification to the listener MBean
162
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
163     }
164
165     /**
166      * @return JDBC connections maximum age
167      */

168     public Integer JavaDoc getJdbcConnMaxAge() {
169         return new Integer JavaDoc(pool.getMaxAge());
170     }
171     /**
172      * @param mn JDBC connections maximum age
173      */

174     public void setJdbcConnMaxAge(Integer JavaDoc mn) {
175         pool.setMaxAge(mn.intValue());
176         String JavaDoc propName = DataBaseServiceImpl.CONNMAXAGE;
177         ReconfiguredProp prop = new ReconfiguredProp(propName, mn.toString());
178         // Send a reconfiguration notification to the listener MBean
179
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
180     }
181
182     /**
183      * @return max maximum size of JDBC connection pool
184      */

185     public Integer JavaDoc getJdbcMaxConnPool() {
186         return new Integer JavaDoc(pool.getPoolMax());
187     }
188     /**
189      * @param max maximum size of JDBC connection pool
190      */

191     public void setJdbcMaxConnPool(Integer JavaDoc max) {
192         pool.setPoolMax(max.intValue());
193         // Update the configuration file
194
String JavaDoc propName = DataBaseServiceImpl.MAXCONPOOL;
195         ReconfiguredProp prop = new ReconfiguredProp(propName, max.toString());
196         // Send a reconfiguration notification to the listener MBean
197
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
198     }
199
200     /**
201      * @return maximum opening time of JDBC connections
202      */

203     public Integer JavaDoc getJdbcMaxOpenTime() {
204         return new Integer JavaDoc(pool.getMaxOpenTime());
205     }
206     /**
207      * @param mn maximum opening time in minutes for JDBC connections
208      */

209     public void setJdbcMaxOpenTime(Integer JavaDoc mn) {
210         pool.setMaxOpenTime(mn.intValue());
211         String JavaDoc propName = DataBaseServiceImpl.MAXOPENTIME;
212         ReconfiguredProp prop = new ReconfiguredProp(propName, mn.toString());
213         // Send a reconfiguration notification to the listener MBean
214
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
215     }
216
217     /**
218      * @return maximum nb of waiters allowed
219      */

220     public Integer JavaDoc getJdbcMaxWaiters() {
221         return new Integer JavaDoc(pool.getMaxWaiters());
222     }
223     /**
224      * @param max maximum nb of waiters allowed
225      */

226     public void setJdbcMaxWaiters(Integer JavaDoc max) {
227         pool.setMaxWaiters(max.intValue());
228         // Update the configuration file
229
String JavaDoc propName = DataBaseServiceImpl.MAXWAITERS;
230         ReconfiguredProp prop = new ReconfiguredProp(propName, max.toString());
231         // Send a reconfiguration notification to the listener MBean
232
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
233     }
234
235     /**
236      * @return maximum time to wait for a connection, in seconds
237      */

238     public Integer JavaDoc getJdbcMaxWaitTime() {
239         return new Integer JavaDoc(pool.getMaxWaitTime());
240     }
241
242     /**
243      * @param max maximum time to wait for a connection, in seconds
244      */

245     public void setJdbcMaxWaitTime(Integer JavaDoc max) {
246         pool.setMaxWaitTime(max.intValue());
247         // Update the configuration file
248
String JavaDoc propName = DataBaseServiceImpl.MAXWAITTIME;
249         ReconfiguredProp prop = new ReconfiguredProp(propName, max.toString());
250         // Send a reconfiguration notification to the listener MBean
251
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
252     }
253
254     /**
255      * @return minimum size of connection pool
256      */

257     public Integer JavaDoc getJdbcMinConnPool() {
258         return new Integer JavaDoc(pool.getPoolMin());
259     }
260     /**
261      * MBean method allowing to set the minimum size of connection pool
262      * @param min minimum size of connection pool
263      */

264     public void setJdbcMinConnPool(Integer JavaDoc min) {
265         pool.setPoolMin(min.intValue());
266         // Update the configuration file
267
String JavaDoc propName = DataBaseServiceImpl.MINCONPOOL;
268         ReconfiguredProp prop = new ReconfiguredProp(propName, min.toString());
269         // Send a reconfiguration notification to the listener MBean
270
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
271     }
272
273     /**
274      * @return sampling period for refresching pool statistics
275      */

276     public Integer JavaDoc getJdbcSamplingPeriod() {
277         return new Integer JavaDoc(pool.getSamplingPeriod());
278     }
279
280     /**
281      * @param i sampling period for refresching pool statistics
282      */

283     public void setJdbcSamplingPeriod(Integer JavaDoc i) {
284         pool.setSamplingPeriod(i.intValue());
285         // Update the configuration file
286
String JavaDoc propName = DataBaseServiceImpl.SAMPLINGPERIOD;
287         ReconfiguredProp prop = new ReconfiguredProp(propName, i.toString());
288         // Send a reconfiguration notification to the listener MBean
289
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
290     }
291
292     /**
293      * @return SQL query for JDBC connections test
294      */

295     public String JavaDoc getJdbcTestStatement() {
296         return pool.getTestStatement();
297     }
298     /**
299      * @param test SQL query for JDBC connections test
300      */

301     public void setJdbcTestStatement(String JavaDoc test) {
302         pool.setTestStatement(test);
303         String JavaDoc propName = DataBaseServiceImpl.CONNTESTSTMT;
304         ReconfiguredProp prop = new ReconfiguredProp(propName, test);
305         // Send a reconfiguration notification to the listener MBean
306
sendReconfigNotification(getSequenceNumber(), datasourceName, prop);
307     }
308
309     // JDBC Connection Pool Statistics
310

311     /**
312      * @return number of connection failures
313      */

314     public Integer JavaDoc getConnectionFailures() {
315         return new Integer JavaDoc(pool.getConnectionFailures());
316     }
317     /**
318      * @return number of connection leaks
319      */

320     public Integer JavaDoc getConnectionLeaks() {
321         return new Integer JavaDoc(pool.getConnectionLeaks());
322     }
323     /**
324      * @return number of busy connections
325      */

326     public Integer JavaDoc getCurrentBusy() {
327         return new Integer JavaDoc(pool.getCurrentBusy());
328     }
329     /**
330      * @return number of busy connections
331      */

332     public Integer JavaDoc getBusyMax() {
333         return new Integer JavaDoc(pool.getBusyMaxRecent());
334     }
335     /**
336      * @return number of busy connections
337      */

338     public Integer JavaDoc getBusyMin() {
339         return new Integer JavaDoc(pool.getBusyMinRecent());
340     }
341     /**
342      * @return number of connections used in transactions
343      */

344     public Integer JavaDoc getCurrentInTx() {
345         return new Integer JavaDoc(pool.getCurrentInTx());
346     }
347     /**
348      * @return number of opened connections
349      */

350     public Integer JavaDoc getCurrentOpened() {
351         return new Integer JavaDoc(pool.getCurrentOpened());
352     }
353     /**
354      * @return current number of connection waiters
355      */

356     public Integer JavaDoc getCurrentWaiters() {
357         return new Integer JavaDoc(pool.getCurrentWaiters());
358     }
359     /**
360      * @return number of opened physical JDBC connections
361      */

362     public Integer JavaDoc getOpenedCount() {
363         return new Integer JavaDoc(pool.getOpenedCount());
364     }
365     /**
366      * @return number of open calls that were rejected because too many waiters
367      */

368     public Integer JavaDoc getRejectedFull() {
369         return new Integer JavaDoc(pool.getRejectedFull());
370     }
371     /**
372      * @return total number of open calls that were rejected
373      */

374     public Integer JavaDoc getRejectedOpen() {
375         return new Integer JavaDoc(pool.getRejectedOpen());
376     }
377     /**
378      * @return number of open calls that were rejected by an unknown reason
379      */

380     public Integer JavaDoc getRejectedOther() {
381         return new Integer JavaDoc(pool.getRejectedOther());
382     }
383     /**
384      * @return number of open calls that were rejected by timeout
385      */

386     public Integer JavaDoc getRejectedTimeout() {
387         return new Integer JavaDoc(pool.getRejectedTimeout());
388     }
389     /**
390      * @return number of xa connection served
391      */

392     public Integer JavaDoc getServedOpen() {
393         return new Integer JavaDoc(pool.getServedOpen());
394     }
395     /**
396      * @return total number of waiters since datasource creation.
397      */

398     public Integer JavaDoc getWaiterCount() {
399         return new Integer JavaDoc(pool.getWaiterCount());
400     }
401     /**
402      * @return Maximum number of waiters since datasource creation.
403      */

404     public Integer JavaDoc getWaitersHigh() {
405         return new Integer JavaDoc(pool.getWaitersHigh());
406     }
407     /**
408      * @return Maximum nb of waiters in last sampling period
409      */

410     public Integer JavaDoc getWaitersHighRecent() {
411         return new Integer JavaDoc(pool.getWaitersHighRecent());
412     }
413     /**
414      * @return Maximum waiting time (millisec) since datasource creation.
415      */

416     public Long JavaDoc getWaitingHigh() {
417         return new Long JavaDoc(pool.getWaitingHigh());
418     }
419     /**
420      * @return Maximum waiting time (millisec) in last sampling period
421      */

422     public Long JavaDoc getWaitingHighRecent() {
423         return new Long JavaDoc(pool.getWaitingHighRecent());
424     }
425     /**
426      * @return Total waiting time (millisec) since datasource creation.
427      */

428     public Long JavaDoc getWaitingTime() {
429         return new Long JavaDoc(pool.getWaitingTime());
430     }
431     /**
432      * Gets the sequence number for reconfiguration opeartions
433      * @return the sequence number for reconfiguration operations
434      */

435     protected long getSequenceNumber() {
436         return ++sequenceNumber;
437     }
438
439     /**
440      * save updated configuration
441      */

442     public void saveConfig() {
443         sendSaveNotification(getSequenceNumber(), datasourceName);
444     }
445 }
446
Popular Tags