You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Susan Cline <ho...@pacbell.net> on 2006/09/08 20:17:31 UTC

Commons DBCP - Shutting down a Derby database without calling DriverManager.getConnection() ?

Hi,

I would like to use Commons DBCP with Tomcat to access a Derby database,
which is working correctly, but the only catch is I would like a clean
way to shutdown the Derby database.

At least two ways to cleanly shut down derby are shown below, listed
by driver class name.

org.apache.derby.jdbc.EmbeddedDriver: 
DriverManager.getConnection("jdbc:derby:;shutdown=true");

org.apache.derby.jdbc.EmbeddedDataSource:
((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
((EmbeddedDataSource)ds).getConnection();

The question I have is, if I want to use Commons DBCP as the factory for 
a DataSource in Tomcat I believe I have to use the EmbeddedDriver
and not the EmbeddedDataSource class, because one of the required
parameters for the BasicAtaSourceFactory is the url parameter as shown
below in my Tomcat server.xml.  (And the EmbeddedDataSource class does 
not use a url.)

<!-- Datasource for Derby database -->
<Resource name="jdbc/derby" type="javax.sql.DataSource" auth="Container" 
descrip
tion="Derby airlinesDB database"/>
<!-- Resource parameters for the Derby database -->
   <ResourceParams name="jdbc/derby">
   <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
   </parameter>
   <parameter>
    <name>driverClassName</name>
    <value>org.apache.derby.jdbc.EmbeddedDriver</value>
   </parameter>
   <parameter>
    <name>url</name> 
<value>jdbc:derby:C:/tomcat/jakarta-tomcat-5.0.28/webapps/myapp/airlinesDB</value>
   </parameter>

If I use the dbcp.BasicDataSourceFactory class, then to shutdown
Derby I have to use a rather awkward way to shutdown the database -
first I have to load the driver and then I have to shut it down;
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
DriverManager.getConnection(SHUTDOWN_URL);

If I try to cast the DataSource object I obtain from the JNDI initial
context as shown in this snippet of code;

((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
// the getConnection method must be called after the setShutdownDatabase
// method to actually perform the shutdown of derby
((EmbeddedDataSource)ds).getConnection();

I get a ClassCastException:

java.lang.ClassCastException: org.apache.commons.dbcp.BasicDataSource
  at
org.apache.derby.demo.persistence.DerbyDatabase.shutdown(DerbyDatabase.java:85)
  at
org.apache.derby.demo.persistence.DataFactory.contextDestroyed(DataFactory.java:132)


Would it just be best to use the Derby DataSource classes instead of 
trying to come up with a klugy way to shutdown Derby gracefully using
the DBCP DataSource and Derby's DriverManager.getConnection() method?

Or does anybody have a suggestion of how to do this better?

Thanks for your help!

Susan

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org