KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > jboss > C3P0PooledDataSource


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.jboss;
25
26 import com.mchange.v2.c3p0.*;
27 import com.mchange.v2.log.*;
28 import java.beans.PropertyVetoException JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.Properties JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.Name JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.NameAlreadyBoundException JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39
40 public class C3P0PooledDataSource implements C3P0PooledDataSourceMBean
41 {
42     private final static MLogger logger = MLog.getLogger( C3P0PooledDataSource.class );
43
44     String JavaDoc jndiName;
45
46     ComboPooledDataSource combods = new ComboPooledDataSource();
47
48     private void rebind() throws NamingException JavaDoc
49     { rebind(null); }
50
51     private void rebind(String JavaDoc unbindName) throws NamingException JavaDoc
52     {
53     InitialContext JavaDoc ictx = new InitialContext JavaDoc();
54     if (unbindName != null)
55         ictx.unbind( unbindName );
56     
57     if (jndiName != null)
58     {
59         // Thanks to David D. Kilzer for this code to auto-create
60
// subcontext paths!
61
Name JavaDoc name = ictx.getNameParser( jndiName ).parse( jndiName );
62         Context JavaDoc ctx = ictx;
63         for (int i = 0, max = name.size() - 1; i < max; i++)
64         {
65         try
66         { ctx = ctx.createSubcontext( name.get( i ) ); }
67         catch (NameAlreadyBoundException JavaDoc ignore)
68         { ctx = (Context JavaDoc) ctx.lookup( name.get( i ) ); }
69         }
70
71         ictx.rebind( jndiName, combods );
72     }
73
74
75     }
76
77     // Jndi Setup Names
78
public void setJndiName(String JavaDoc jndiName) throws NamingException JavaDoc
79     {
80     String JavaDoc unbindName = this.jndiName;
81     this.jndiName = jndiName;
82     rebind( unbindName );
83     }
84
85     public String JavaDoc getJndiName()
86     { return jndiName; }
87
88     // DriverManagerDataSourceProperties (count: 4)
89
public String JavaDoc getDescription()
90     { return combods.getDescription(); }
91     
92     public void setDescription( String JavaDoc description ) throws NamingException JavaDoc
93     {
94     combods.setDescription( description );
95     rebind();
96     }
97     
98     public String JavaDoc getDriverClass()
99     { return combods.getDriverClass(); }
100     
101     public void setDriverClass( String JavaDoc driverClass ) throws PropertyVetoException JavaDoc, NamingException JavaDoc
102     {
103     combods.setDriverClass( driverClass );
104     rebind();
105     }
106     
107     public String JavaDoc getJdbcUrl()
108     { return combods.getJdbcUrl(); }
109     
110     public void setJdbcUrl( String JavaDoc jdbcUrl ) throws NamingException JavaDoc
111     {
112     combods.setJdbcUrl( jdbcUrl );
113     rebind();
114     }
115     
116     // DriverManagerDataSource "virtual properties" based on properties
117
public String JavaDoc getUser()
118     { return combods.getUser(); }
119     
120     public void setUser( String JavaDoc user ) throws NamingException JavaDoc
121     {
122     combods.setUser( user );
123     rebind();
124     }
125     
126     public String JavaDoc getPassword()
127     { return combods.getPassword(); }
128     
129     public void setPassword( String JavaDoc password ) throws NamingException JavaDoc
130     {
131     combods.setPassword( password );
132     rebind();
133     }
134
135     // WrapperConnectionPoolDataSource properties (count: 21)
136
public int getCheckoutTimeout()
137     { return combods.getCheckoutTimeout(); }
138     
139     public void setCheckoutTimeout( int checkoutTimeout ) throws NamingException JavaDoc
140     {
141     combods.setCheckoutTimeout( checkoutTimeout );
142     rebind();
143     }
144
145     public int getAcquireIncrement()
146     { return combods.getAcquireIncrement(); }
147     
148     public void setAcquireIncrement( int acquireIncrement ) throws NamingException JavaDoc
149     {
150     combods.setAcquireIncrement( acquireIncrement );
151     rebind();
152     }
153     
154     public int getAcquireRetryAttempts()
155     { return combods.getAcquireRetryAttempts(); }
156     
157     public void setAcquireRetryAttempts( int acquireRetryAttempts ) throws NamingException JavaDoc
158     {
159     combods.setAcquireRetryAttempts( acquireRetryAttempts );
160     rebind();
161     }
162     
163     public int getAcquireRetryDelay()
164     { return combods.getAcquireRetryDelay(); }
165     
166     public void setAcquireRetryDelay( int acquireRetryDelay ) throws NamingException JavaDoc
167     {
168     combods.setAcquireRetryDelay( acquireRetryDelay );
169     rebind();
170     }
171     
172     public boolean isAutoCommitOnClose()
173     { return combods.isAutoCommitOnClose(); }
174
175     public void setAutoCommitOnClose( boolean autoCommitOnClose ) throws NamingException JavaDoc
176     {
177     combods.setAutoCommitOnClose( autoCommitOnClose );
178     rebind();
179     }
180     
181     public String JavaDoc getConnectionTesterClassName()
182     { return combods.getConnectionTesterClassName(); }
183     
184     public void setConnectionTesterClassName( String JavaDoc connectionTesterClassName ) throws PropertyVetoException JavaDoc, NamingException JavaDoc
185     {
186     combods.setConnectionTesterClassName( connectionTesterClassName );
187     rebind();
188     }
189     
190     public String JavaDoc getAutomaticTestTable()
191     { return combods.getAutomaticTestTable(); }
192     
193     public void setAutomaticTestTable( String JavaDoc automaticTestTable ) throws NamingException JavaDoc
194     {
195     combods.setAutomaticTestTable( automaticTestTable );
196     rebind();
197     }
198     
199     public boolean isForceIgnoreUnresolvedTransactions()
200     { return combods.isForceIgnoreUnresolvedTransactions(); }
201     
202     public void setForceIgnoreUnresolvedTransactions( boolean forceIgnoreUnresolvedTransactions ) throws NamingException JavaDoc
203     {
204     combods.setForceIgnoreUnresolvedTransactions( forceIgnoreUnresolvedTransactions );
205     rebind();
206     }
207     
208     public int getIdleConnectionTestPeriod()
209     { return combods.getIdleConnectionTestPeriod(); }
210     
211     public void setIdleConnectionTestPeriod( int idleConnectionTestPeriod ) throws NamingException JavaDoc
212     {
213     combods.setIdleConnectionTestPeriod( idleConnectionTestPeriod );
214     rebind();
215     }
216     
217     public int getInitialPoolSize()
218     { return combods.getInitialPoolSize(); }
219     
220     public void setInitialPoolSize( int initialPoolSize ) throws NamingException JavaDoc
221     {
222     combods.setInitialPoolSize( initialPoolSize );
223     rebind();
224     }
225
226     public int getMaxIdleTime()
227     { return combods.getMaxIdleTime(); }
228     
229     public void setMaxIdleTime( int maxIdleTime ) throws NamingException JavaDoc
230     {
231     combods.setMaxIdleTime( maxIdleTime );
232     rebind();
233     }
234     
235     public int getMaxPoolSize()
236     { return combods.getMaxPoolSize(); }
237     
238     public void setMaxPoolSize( int maxPoolSize ) throws NamingException JavaDoc
239     {
240     combods.setMaxPoolSize( maxPoolSize );
241     rebind();
242     }
243     
244     public int getMaxStatements()
245     { return combods.getMaxStatements(); }
246     
247     public void setMaxStatements( int maxStatements ) throws NamingException JavaDoc
248     {
249     combods.setMaxStatements( maxStatements );
250     rebind();
251     }
252     
253     public int getMaxStatementsPerConnection()
254     { return combods.getMaxStatementsPerConnection(); }
255     
256     public void setMaxStatementsPerConnection( int maxStatementsPerConnection ) throws NamingException JavaDoc
257     {
258     combods.setMaxStatementsPerConnection( maxStatementsPerConnection );
259     rebind();
260     }
261     
262     public int getMinPoolSize()
263     { return combods.getMinPoolSize(); }
264     
265     public void setMinPoolSize( int minPoolSize ) throws NamingException JavaDoc
266     {
267     combods.setMinPoolSize( minPoolSize );
268     rebind();
269     }
270     
271     public int getPropertyCycle()
272     { return combods.getPropertyCycle(); }
273     
274     public void setPropertyCycle( int propertyCycle ) throws NamingException JavaDoc
275     {
276     combods.setPropertyCycle( propertyCycle );
277     rebind();
278     }
279     
280     public boolean isBreakAfterAcquireFailure()
281     { return combods.isBreakAfterAcquireFailure(); }
282     
283     public void setBreakAfterAcquireFailure( boolean breakAfterAcquireFailure ) throws NamingException JavaDoc
284     {
285     combods.setBreakAfterAcquireFailure( breakAfterAcquireFailure );
286     rebind();
287     }
288     
289     public boolean isTestConnectionOnCheckout()
290     { return combods.isTestConnectionOnCheckout(); }
291     
292     public void setTestConnectionOnCheckout( boolean testConnectionOnCheckout ) throws NamingException JavaDoc
293     {
294     combods.setTestConnectionOnCheckout( testConnectionOnCheckout );
295     rebind();
296     }
297     
298     public boolean isTestConnectionOnCheckin()
299     { return combods.isTestConnectionOnCheckin(); }
300     
301     public void setTestConnectionOnCheckin( boolean testConnectionOnCheckin ) throws NamingException JavaDoc
302     {
303     combods.setTestConnectionOnCheckin( testConnectionOnCheckin );
304     rebind();
305     }
306     
307     public boolean isUsesTraditionalReflectiveProxies()
308     { return combods.isUsesTraditionalReflectiveProxies(); }
309     
310     public void setUsesTraditionalReflectiveProxies( boolean usesTraditionalReflectiveProxies ) throws NamingException JavaDoc
311     {
312     combods.setUsesTraditionalReflectiveProxies( usesTraditionalReflectiveProxies );
313     rebind();
314     }
315
316     public String JavaDoc getPreferredTestQuery()
317     { return combods.getPreferredTestQuery(); }
318     
319     public void setPreferredTestQuery( String JavaDoc preferredTestQuery ) throws NamingException JavaDoc
320     {
321     combods.setPreferredTestQuery( preferredTestQuery );
322     rebind();
323     }
324
325     // PoolBackedDataSource properties (count: 2)
326
public String JavaDoc getDataSourceName()
327     { return combods.getDataSourceName(); }
328     
329     public void setDataSourceName( String JavaDoc name ) throws NamingException JavaDoc
330     {
331     combods.setDataSourceName( name );
332     rebind();
333     }
334
335     public int getNumHelperThreads()
336     { return combods.getNumHelperThreads(); }
337     
338     public void setNumHelperThreads( int numHelperThreads ) throws NamingException JavaDoc
339     {
340     combods.setNumHelperThreads( numHelperThreads );
341     rebind();
342     }
343
344     // shared properties (count: 1)
345
public String JavaDoc getFactoryClassLocation()
346     { return combods.getFactoryClassLocation(); }
347     
348     public void setFactoryClassLocation( String JavaDoc factoryClassLocation ) throws NamingException JavaDoc
349     {
350     combods.setFactoryClassLocation( factoryClassLocation );
351     rebind();
352     }
353
354     // PooledDataSource statistics
355

356     public int getNumUserPools() throws SQLException JavaDoc
357     { return combods.getNumUserPools(); }
358
359     public int getNumConnectionsDefaultUser() throws SQLException JavaDoc
360     { return combods.getNumConnectionsDefaultUser(); }
361
362     public int getNumIdleConnectionsDefaultUser() throws SQLException JavaDoc
363     { return combods.getNumIdleConnectionsDefaultUser(); }
364
365     public int getNumBusyConnectionsDefaultUser() throws SQLException JavaDoc
366     { return combods.getNumBusyConnectionsDefaultUser(); }
367
368     public int getNumUnclosedOrphanedConnectionsDefaultUser() throws SQLException JavaDoc
369     { return combods.getNumUnclosedOrphanedConnectionsDefaultUser(); }
370
371     public int getNumConnections(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
372     { return combods.getNumConnections(username, password); }
373
374     public int getNumIdleConnections(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
375     { return combods.getNumIdleConnections(username, password); }
376
377     public int getNumBusyConnections(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
378     { return combods.getNumBusyConnections(username, password); }
379
380     public int getNumUnclosedOrphanedConnections(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
381     { return combods.getNumUnclosedOrphanedConnections(username, password); }
382
383     public int getNumConnectionsAllUsers() throws SQLException JavaDoc
384     { return combods.getNumConnectionsAllUsers(); }
385
386     public int getNumIdleConnectionsAllUsers() throws SQLException JavaDoc
387     { return combods.getNumIdleConnectionsAllUsers(); }
388
389     public int getNumBusyConnectionsAllUsers() throws SQLException JavaDoc
390     { return combods.getNumBusyConnectionsAllUsers(); }
391
392     public int getNumUnclosedOrphanedConnectionsAllUsers() throws SQLException JavaDoc
393     { return combods.getNumUnclosedOrphanedConnectionsAllUsers(); }
394
395     // PooledDataSource operations
396
public void softResetDefaultUser() throws SQLException JavaDoc
397     { combods.softResetDefaultUser(); }
398
399     public void softReset(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
400     { combods.softReset(username, password); }
401
402     public void softResetAllUsers() throws SQLException JavaDoc
403     { combods.softResetAllUsers(); }
404
405     public void hardReset() throws SQLException JavaDoc
406     { combods.hardReset(); }
407
408     public void close() throws SQLException JavaDoc
409     { combods.close(); }
410
411     //JBoss only... (but these methods need not be called for the mbean to work)
412
public void create() throws Exception JavaDoc
413     { }
414
415     // the mbean works without this, but if called we start populating the pool early
416
public void start() throws Exception JavaDoc
417     {
418     //System.err.println("Bound C3P0 PooledDataSource to name '" + jndiName + "'. Starting...");
419
logger.log(MLevel.INFO, "Bound C3P0 PooledDataSource to name ''{0}''. Starting...", jndiName);
420     combods.getNumBusyConnectionsDefaultUser(); //just touch the datasource to start it up.
421
}
422
423
424     public void stop()
425     { }
426
427     public void destroy()
428     {
429         try
430         {
431             combods.close();
432             logger.log(MLevel.INFO, "Destroyed C3P0 PooledDataSource with name ''{0}''.", jndiName);
433         }
434         catch (Exception JavaDoc e)
435         {
436             logger.log(MLevel.INFO, "Failed to destroy C3P0 PooledDataSource.", e);
437         }
438     }
439
440     public String JavaDoc getConnectionCustomizerClassName()
441     { return combods.getConnectionCustomizerClassName(); }
442
443     public float getEffectivePropertyCycle(String JavaDoc username, String JavaDoc password) throws SQLException JavaDoc
444     { return combods.getEffectivePropertyCycle(username, password); }
445
446     public float getEffectivePropertyCycleDefaultUser() throws SQLException JavaDoc
447     { return combods.getEffectivePropertyCycleDefaultUser(); }
448
449     public int getMaxAdministrativeTaskTime()
450     { return combods.getMaxAdministrativeTaskTime(); }
451
452     public int getMaxConnectionAge()
453     { return combods.getMaxConnectionAge(); }
454
455     public int getMaxIdleTimeExcessConnections()
456     { return combods.getMaxIdleTimeExcessConnections(); }
457
458     public int getUnreturnedConnectionTimeout()
459     { return combods.getUnreturnedConnectionTimeout(); }
460
461     public boolean isDebugUnreturnedConnectionStackTraces()
462     { return combods.isDebugUnreturnedConnectionStackTraces(); }
463
464     public void setConnectionCustomizerClassName(String JavaDoc connectionCustomizerClassName) throws NamingException JavaDoc
465     {
466         combods.setConnectionCustomizerClassName(connectionCustomizerClassName);
467         rebind();
468     }
469
470     public void setDebugUnreturnedConnectionStackTraces(boolean debugUnreturnedConnectionStackTraces) throws NamingException JavaDoc
471     {
472         combods.setDebugUnreturnedConnectionStackTraces(debugUnreturnedConnectionStackTraces);
473         rebind();
474     }
475
476     public void setMaxAdministrativeTaskTime(int maxAdministrativeTaskTime) throws NamingException JavaDoc
477     {
478         combods.setMaxAdministrativeTaskTime(maxAdministrativeTaskTime);
479         rebind();
480     }
481
482     public void setMaxConnectionAge(int maxConnectionAge) throws NamingException JavaDoc
483     {
484         combods.setMaxConnectionAge( maxConnectionAge );
485         rebind();
486     }
487
488     public void setMaxIdleTimeExcessConnections(int maxIdleTimeExcessConnections) throws NamingException JavaDoc
489     {
490         combods.setMaxIdleTimeExcessConnections(maxIdleTimeExcessConnections);
491         rebind();
492     }
493
494     public void setUnreturnedConnectionTimeout(int unreturnedConnectionTimeout) throws NamingException JavaDoc
495     {
496         combods.setUnreturnedConnectionTimeout(unreturnedConnectionTimeout);
497         rebind();
498     }
499 }
500
501
Popular Tags