You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by "Susan L. Cline" <ho...@pacbell.net> on 2006/09/07 18:24:19 UTC

Using Apache DBCP and Tomcat with Derby - database shutdown?

Hi,

This is not strictly a Derby question, but I'm hoping someone else has used Apache DBCP
with Tomcat and Derby.  

According to the Tomcat 5.0 docs:

The default data source support in Tomcat is based on the DBCP connection pool from the Jakarta
Commons subproject.

and here is some sample code to obtain this JNDI data source:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB");

Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

I'm using this code to obtain my connections to Derby, however the question I have is how to
shutdown Derby correctly when I shut down my web application?

My class that implements the ServletContextListener interface calls this shutdown method of
my application class to shutdown Derby:

try {
	System.out.println("Derby database shutting down.");
	DriverManager.getConnection(SHUTDOWN_URL);
} catch (Exception except) {
	System.out.println(except.getMessage());
}

But this does not seem correct because I'm obtaining the connections to Derby via the datasource
and a pool, and here I am shutting the database down using the DriverManager class.  Is this okay?

I can't find anything in the DBCP documentation to help me shutdown the database cleanly.

Does anyone have any suggestions on how to do this?

If I do not get a response from this mailing list, I'll try the Apache DBCP mailing list or maybe
Tomcat as well, but I thought I'd check here first.

Thanks,

Susan

Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by Daniel John Debrunner <dj...@apache.org>.

> --- Daniel John Debrunner <dj...@apache.org> wrote:

> 
>>   2) Some J2EE app servers will wrap the database's DataSource
>>implementation in their own, meaning the cases will fail.

Sorry I mistyped, I mean "the casts will fail".

Dan.



Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by "Susan L. Cline" <ho...@pacbell.net>.
--- Daniel John Debrunner <dj...@apache.org> wrote:

> home4slc@pacbell.net wrote:
> 
> > Hi Deepa,
> >  
> > Thanks a lot for your help.  That worked like a charm.
> >  
> > So I have now changed my code to this:
> >  
> >    ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
> >    ((EmbeddedDataSource)ds).getConnection();
> 
> Just a couple of warnings with this code:
> 
>    1) This affects the datasource for every application or thread that
> is using it.

That makes sense, in this case this is what I want.  The method that this code
is wrapped in is called when Tomcat is shutdown.


>    2) Some J2EE app servers will wrap the database's DataSource
> implementation in their own, meaning the cases will fail.

Sorry, not sure what you mean 'the cases will fail'?  However, as I mentioned
in my next email, it turns out this does not work because of the way I have Tomcat
server.xml set up.  I have it configured to use the Apache DBCP connection pool
class instead of the Derby EmbeddedDataSource class.  But I am pointing to the
Derby EmbeddedDriver class as the driver class name.  It seems like if I want to
use the Apache DBCP connection pooling I have to use the Derby EmbeddedDriver because
one of the parameters DBCP requires is a URL parameter, which leads me back to my
original question.

> 
> Another way is to have another Derby data source setup in the
> configuration that has the shutdownDatabase property set to "shutdown".
> Then you would do:
> 
> Context initCtx = new InitialContext();
> Context envCtx = (Context) initCtx.lookup("java:comp/env");
> DataSource ds = (DataSource)
>   envCtx.lookup("jdbc/EmployeeDB_shutdown");
> 
> try {
>   ds.getConnection();
> } catch (SQLExcepion sqle) {
> }
> 

Good suggestion, thanks.

> Dan.
> 
> 
> 


Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by Daniel John Debrunner <dj...@apache.org>.
home4slc@pacbell.net wrote:

> Hi Deepa,
>  
> Thanks a lot for your help.  That worked like a charm.
>  
> So I have now changed my code to this:
>  
>    ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
>    ((EmbeddedDataSource)ds).getConnection();

Just a couple of warnings with this code:

   1) This affects the datasource for every application or thread that
is using it.
   2) Some J2EE app servers will wrap the database's DataSource
implementation in their own, meaning the cases will fail.

Another way is to have another Derby data source setup in the
configuration that has the shutdownDatabase property set to "shutdown".
Then you would do:

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)
  envCtx.lookup("jdbc/EmployeeDB_shutdown");

try {
  ds.getConnection();
} catch (SQLExcepion sqle) {
}

Dan.



Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by ho...@pacbell.net.

----- Original Message ----
From: home4slc@pacbell.net
To: Derby Discussion <de...@db.apache.org>
Sent: Thursday, September 7, 2006 2:56:02 PM
Subject: Re: Using Apache DBCP and Tomcat with Derby - database shutdown?


Hi Deepa,

Thanks a lot for your help.  That worked like a charm.

So I have now changed my code to this:

   ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
   ((EmbeddedDataSource)ds).getConnection();

Thanks,

Susan
----- Original Message ----
From: Deepa Remesh <dr...@gmail.com>
To: Derby Discussion <de...@db.apache.org>
Sent: Thursday, September 7, 2006 9:54:08 AM
Subject: Re: Using Apache DBCP and Tomcat with Derby - database shutdown?


On 9/7/06, Susan L. Cline <ho...@pacbell.net> wrote:
[snip]
>
> But this does not seem correct because I'm obtaining the connections to Derby via the datasource
> and a pool, and here I am shutting the database down using the DriverManager class.  Is this okay?
>
> I can't find anything in the DBCP documentation to help me shutdown the database cleanly.
>
To shutdown using Derby DataSource object, you may set
"ShutdownDatabase" property by calling setShutdownDatabase("shutdown")
method of data source. After setting this property, a call to
getConnection method of data source will shutdown the database. Hope
this helps.


This information is in
http://db.apache.org/derby/docs/dev/devguide/cdevresman19524.html

There is an example which shows use of "CreateDatabase" property. Use
of "ShutdownDatabase" property is similar to this. I had recently
reviewed this section of developer guide. As you are using Derby in
J2EE environment, it would be nice if you can also provide any
suggestions to improve this section of the documentation.

Thanks,
Deepa

Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by ho...@pacbell.net.
Whoops, sorry about that last reply .. .sticky fingers.  I was too hasty on my previous(previous) response.
Although the below appears to work it does not.  The reason being in my Tomcat server.xml I am trying
to use the Apache DBCP Basic Data source.
 
<Resource name="jdbc/derby" type="javax.sql.DataSource" auth="Container" description="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> 
 
...
 
When I try to shut down the database using the code below I get this exception:
 
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)
 
I suppose I could change the factory parameter to org.apache.derby.jdbc.EmbeddedDataSource, but as I 
mentioned I wanted to use the DBCP BasicDataSourceFactory.  
 
Any other thoughts?
 
Thanks,
 
Susan

----- Original Message ----
From: home4slc@pacbell.net
To: Derby Discussion <de...@db.apache.org>
Sent: Thursday, September 7, 2006 2:56:02 PM
Subject: Re: Using Apache DBCP and Tomcat with Derby - database shutdown?


Hi Deepa,

Thanks a lot for your help.  That worked like a charm.

So I have now changed my code to this:

   ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
   ((EmbeddedDataSource)ds).getConnection();

Thanks,

Susan
----- Original Message ----
From: Deepa Remesh <dr...@gmail.com>
To: Derby Discussion <de...@db.apache.org>
Sent: Thursday, September 7, 2006 9:54:08 AM
Subject: Re: Using Apache DBCP and Tomcat with Derby - database shutdown?


On 9/7/06, Susan L. Cline <ho...@pacbell.net> wrote:
[snip]
>
> But this does not seem correct because I'm obtaining the connections to Derby via the datasource
> and a pool, and here I am shutting the database down using the DriverManager class.  Is this okay?
>
> I can't find anything in the DBCP documentation to help me shutdown the database cleanly.
>
To shutdown using Derby DataSource object, you may set
"ShutdownDatabase" property by calling setShutdownDatabase("shutdown")
method of data source. After setting this property, a call to
getConnection method of data source will shutdown the database. Hope
this helps.


This information is in
http://db.apache.org/derby/docs/dev/devguide/cdevresman19524.html

There is an example which shows use of "CreateDatabase" property. Use
of "ShutdownDatabase" property is similar to this. I had recently
reviewed this section of developer guide. As you are using Derby in
J2EE environment, it would be nice if you can also provide any
suggestions to improve this section of the documentation.

Thanks,
Deepa

Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by ho...@pacbell.net.
Hi Deepa,
 
Thanks a lot for your help.  That worked like a charm.
 
So I have now changed my code to this:
 
   ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
   ((EmbeddedDataSource)ds).getConnection();
 
Thanks,
 
Susan
----- Original Message ----
From: Deepa Remesh <dr...@gmail.com>
To: Derby Discussion <de...@db.apache.org>
Sent: Thursday, September 7, 2006 9:54:08 AM
Subject: Re: Using Apache DBCP and Tomcat with Derby - database shutdown?


On 9/7/06, Susan L. Cline <ho...@pacbell.net> wrote:
[snip]
>
> But this does not seem correct because I'm obtaining the connections to Derby via the datasource
> and a pool, and here I am shutting the database down using the DriverManager class.  Is this okay?
>
> I can't find anything in the DBCP documentation to help me shutdown the database cleanly.
>
To shutdown using Derby DataSource object, you may set
"ShutdownDatabase" property by calling setShutdownDatabase("shutdown")
method of data source. After setting this property, a call to
getConnection method of data source will shutdown the database. Hope
this helps.


This information is in
http://db.apache.org/derby/docs/dev/devguide/cdevresman19524.html

There is an example which shows use of "CreateDatabase" property. Use
of "ShutdownDatabase" property is similar to this. I had recently
reviewed this section of developer guide. As you are using Derby in
J2EE environment, it would be nice if you can also provide any
suggestions to improve this section of the documentation.

Thanks,
Deepa

Re: Using Apache DBCP and Tomcat with Derby - database shutdown?

Posted by Deepa Remesh <dr...@gmail.com>.
On 9/7/06, Susan L. Cline <ho...@pacbell.net> wrote:
[snip]
>
> But this does not seem correct because I'm obtaining the connections to Derby via the datasource
> and a pool, and here I am shutting the database down using the DriverManager class.  Is this okay?
>
> I can't find anything in the DBCP documentation to help me shutdown the database cleanly.
>
To shutdown using Derby DataSource object, you may set
"ShutdownDatabase" property by calling setShutdownDatabase("shutdown")
method of data source. After setting this property, a call to
getConnection method of data source will shutdown the database. Hope
this helps.

This information is in
http://db.apache.org/derby/docs/dev/devguide/cdevresman19524.html

There is an example which shows use of "CreateDatabase" property. Use
of "ShutdownDatabase" property is similar to this. I had recently
reviewed this section of developer guide. As you are using Derby in
J2EE environment, it would be nice if you can also provide any
suggestions to improve this section of the documentation.

Thanks,
Deepa