load the driver explicitly
[jabaws.git] / webservices / compbio / stat / collector / StatDB.java
index 08b99f9..b469c44 100644 (file)
@@ -1,3 +1,20 @@
+/* Copyright (c) 2011 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
 package compbio.stat.collector;\r
 \r
 import java.sql.Connection;\r
@@ -41,19 +58,26 @@ public class StatDB {
                if (conn != null && !conn.isClosed()) {\r
                        return conn;\r
                } else {\r
-                       String dbpath = PropertyHelperManager.getLocalPath();\r
-                       log.info("Looking for JABAWS access statistics database at: "\r
-                                       + dbpath);\r
-                       System.setProperty("derby.system.home", dbpath);\r
-                       conn = DriverManager.getConnection(protocol + statDBName\r
-                                       + ";create=false");\r
-\r
-                       conn.setAutoCommit(true);\r
-                       /*\r
-                        * Runtime.getRuntime().addShutdownHook(new Thread() {\r
-                        * \r
-                        * @Override public void run() { shutdownDBServer(); } });\r
-                        */\r
+                       try {\r
+                               String dbpath = PropertyHelperManager.getLocalPath();\r
+                               log.info("Looking for JABAWS access statistics database at: "\r
+                                               + dbpath);\r
+                               System.setProperty("derby.system.home", dbpath);\r
+                               // Apparently under Tomcat webapp you cannot rely on Java\r
+                               // auto discovery and have to register the driver explicitly\r
+                               Class.forName(driver);\r
+                               conn = DriverManager.getConnection(protocol + statDBName\r
+                                               + ";create=false");\r
+\r
+                               conn.setAutoCommit(true);\r
+                               /*\r
+                                * Runtime.getRuntime().addShutdownHook(new Thread() {\r
+                                * \r
+                                * @Override public void run() { shutdownDBServer(); } });\r
+                                */\r
+                       } catch (ClassNotFoundException e) {\r
+                               log.error(e.getMessage(), e);\r
+                       }\r
                }\r
                return conn;\r
        }\r
@@ -338,9 +362,15 @@ public class StatDB {
                        log.info("Database shut down normally");\r
                }\r
        }\r
-       public static void main(String[] args) throws SQLException {\r
+       public static void main(String[] args) {\r
                // This is called from Ant cleanStatTable task\r
-               clearStatTable();\r
+               try {\r
+                       clearStatTable();\r
+                       shutdownDBServer();\r
+               } catch (SQLException e) {\r
+                       System.err.println("Fails to clean up JABAWS stat database!");\r
+                       e.printStackTrace();\r
+               }\r
                // new StatDB().createStatTable();\r
                // insertData(null);\r
                /*\r