db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Albert Kam" <moonblade.w...@gmail.com>
Subject Re: Shutdown cleanly embedded derby when tomcat shutdown ?
Date Fri, 11 Jul 2008 08:21:42 GMT
Dear all ..

Ah, finally, a solution that works !

I use this resource in the webapps/mywebapp/META-INF/context.xml to shutdown
derby (so in the context.xml, i got 2 datasources, 1 is the normal
datasource, and this one is to shut derby down):

        <Resource name="jdbc/SofcoSMSShutdown" auth="Container"
              type="javax.sql.DataSource"
              maxActive="1" maxIdle="30" maxWait="10000"
              username="app" password="a"
              driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
              url="jdbc:derby:Databases/sms;shutdownDatabase=shutdown"/>

I just need to add this in the web.xml
  <resource-ref>
      <description>Shutdown SofcoSMS DataSource</description>
      <res-ref-name>jdbc/SofcoSMSShutdown</res-ref-name>
      <res-type>org.apache.derby.jdbc.EmbeddedDataSource</res-type>
      <res-auth>Container</res-auth>
      <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

and use it in the source code like this :
    public void shutdownDB() {
        Context initCtx;
        try {
            log.debug("shutting down database from datasource");
            initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            ds = (DataSource) envCtx.lookup("jdbc/SofcoSMSShutdown");
            ds.getConnection();
            ds = null;
            log.debug("database shutdown complete");
        } catch (NamingException e) {
            log.error(e,e);
            throw new InfrastructureException(e);
        } catch (SQLException e) {
            log.error(e,e);
            throw new InfrastructureException(e);
        }
   }

Also, i put the shutting down process in the apache struts' plugin mechanism
in the destroy() method which will be called when the webapp is being
unloaded by the servlet container.

It works like charm !

Anyway i tried the :
            ((EmbeddedDataSource)ds).setShutdownDatabase("shutdown");
            ((EmbeddedDataSource)ds).getConnection();

But it didnt succeed as i got a ClassCastException just as depicted in this
thread :
http://mail-archives.apache.org/mod_mbox/db-derby-user/200609.mbox/%3C20060907221739.38438.qmail@web81307.mail.mud.yahoo.com%3E

Also, one note for others who get this 'tomcat-hanging or lingering after
shutdown' issue that i found on the web is that to use "kill -3
<tomcat-pid>", and then check the catalina.out for the threads that still
linger. Very helpful in identifying what that we forget to close.

Regards,
Albert Kam

-- 
Do not pursue the past. Do not lose yourself in the future.
The past no longer is. The future has not yet come.
Looking deeply at life as it is in the very here and now,
the practitioner dwells in stability and freedom.
(Thich Nhat Hanh)

Mime
View raw message