tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Awaneesh Shatmanyu" <a.shatma...@zensar.com>
Subject RE: HSQLDB and Tomcat ServletContextListener
Date Thu, 21 Sep 2006 14:58:43 GMT
Also for you information ...the auto-commit of HSQLDB is off by default.

  
 
 

-----Original Message-----
From: Awaneesh Shatmanyu [mailto:a.shatmanyu@zensar.com] 
Sent: Thursday, September 21, 2006 8:15 PM
To: Tomcat Users List
Subject: RE: HSQLDB and Tomcat ServletContextListener

Hi Feris,

I am currently working with the HSQLDB for my project.
I had similar problem, which got solved.
1. Make sure your jdbc connection is closed every time.
2. Do not run you application as long as you are looking into the
HSQLDB,    
   close the DB and then run the application.

Regards,
Awaneesh Shatmanyu

  
 
 
-----Original Message-----
From: Feris Thia [mailto:feris.tia@gmail.com] 
Sent: Thursday, September 21, 2006 7:55 PM
To: Tomcat Users List
Subject: HSQLDB and Tomcat ServletContextListener

Dear All,

 I'm using HSQLDB as embedded in-process mode for my web application. I
used
 to schedule every 1 minutes to check on the HSQLDB database. I open the
db
 when context initialized and close it when context destroyed. But when
the
 context reloaded I always get Java.lang.NullPointerException when
trying to
 execute SQL statement.

 What is wrong ?? It looks like everytime the context is destroyed...
the
 hsqldb gets lock.

 The snippet code like below :
 =======================

 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletContextEvent;

 import java.nio.channels.ClosedByInterruptException;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.util.Random;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;

 public class ContextStartUp implements ServletContextListener {

     private ServletContext context = null;
     MyThread objThread = null;

     ResultSet rs = null;
     Connection conn = null;

     public void openDB()
     {

         try {
             conn = DriverManager.getConnection("jdbc:hsqldb:file:" +
 context.getRealPath("/data/testdb") + ";ifexists=true;shutdown=true;",
"sa",
 "phidmspassword");

         } catch (SQLException e) {
             e.printStackTrace();
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
     }


     public void closeDB()
     {
         Statement stmt = null;
         try {
             if(conn!=null)
             {
                 stmt = conn.createStatement();
                 stmt.execute("SHUTDOWN COMPACT");
                 stmt.close();
                 conn.close();
                 stmt = null;
                 conn = null;
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

     public class MyThread implements Runnable {

         int nomor = 0;

         Thread currentthread = null;

         public MyThread() {
             if (currentthread == null) {
                 currentthread = new Thread(this);
                 currentthread.setPriority(new Random().nextInt(5) + 1);
                 currentthread.start();
             }
         }

         public void run() {
             Statement stmt = null;

             String sqltodo = "";
             Thread myThread = Thread.currentThread();

             while (myThread == currentthread) {
                 try {
                     stmt = conn.createStatement();

                     if (stmt.execute("A select statement...... "))
                     {
                         rs = stmt.getResultSet();

                         if (rs.next()) {
                             ..........................
                         }
                     }


                        sqltodo = Update queries ......;

                         stmt.executeUpdate(sqltodo);
                         stmt.execute("SHUTDOWN");
                         stmt.close();
                     }
                     else
                     {
                         stmt.close();
                     }
                 } catch (SQLException e) {
                     e.printStackTrace();
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {
                     stmt = null;
                 }

                 try {
                     Thread.sleep(100000);
                 } catch (InterruptedException e) {
                 }
             }
         }

     }

     public void initThread() {
         openDB();
         objThread = new MyThread();
     }

     public void stopThread() {
         objThread = null;
         closeDB();
     }

     public void contextDestroyed(ServletContextEvent event) {
         context = null;
         stopThread();
     }

     public void contextInitialized(ServletContextEvent event) {
         try {
             Class.forName("org.hsqldb.jdbcDriver").newInstance();
         } catch (InstantiationException e) {
             e.printStackTrace();
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         }

         context = event.getServletContext();

         initThread();
     }
 }

 ==============================================================


-- 
Regards,
Feris
PT. Putera Handal Indotama
JL. KH. Moh. Mansyur No. 11 Blok B.8-12
Telp. +62-21-631 6688 (Hunting)
Fax. +62-21-6330211
Jakarta (10140) - INDONESIA

This email may contain confidential or privileged information for the 
intended recipient(s) and the views expressed in the same are not 
necessarily the views of Zensar Technologies Ltd. If you are not the
intended 
recipient or have received this e-mail by error, its use is strictly 
prohibited, please delete the e-mail and notify the sender. Zensar 
Technologies Ltd. does not accept any liability for virus infected
mails.


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message