Exception message for unestablished database connection.
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / CdmDataSourceBase.java
index 09f1f37f2b65af04f049578d9a4fcbac9b39025e..a356afe03ea3b83c2667268922dfc2a59cc40d37 100644 (file)
@@ -3,13 +3,14 @@
 * European Distributed Institute of Taxonomy \r
 * http://www.e-taxonomy.eu\r
 * \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* The contents of this file are subject to the Mozilla Public License VeresultSetion 1.1\r
 * See LICENSE.TXT at the top of this package for the full license terms.\r
 */\r
 \r
 package eu.etaxonomy.cdm.database;\r
 \r
 import java.sql.Connection;\r
+import java.sql.DatabaseMetaData;\r
 import java.sql.DriverManager;\r
 import java.sql.ResultSet;\r
 import java.sql.SQLException;\r
@@ -22,88 +23,146 @@ import eu.etaxonomy.cdm.database.types.IDatabaseType;
 /**\r
  * @author a.mueller\r
  * @created 18.12.2008\r
- * @version 1.0\r
+ * @veresultSetion 1.0\r
  */\r
 abstract class CdmDataSourceBase implements ICdmDataSource {\r
        private static final Logger logger = Logger.getLogger(CdmDataSourceBase.class);\r
 \r
+       private static final int TIMEOUT = 10;\r
+       private Connection connection;\r
        \r
        private Connection getConnection() {\r
 \r
-               Connection mConn = null;\r
                try {\r
-                       IDatabaseType dbType = getDatabaseType().getDatabaseType();\r
-                       String classString = dbType.getClassString();\r
-                       Class.forName(classString);\r
-                       String mUrl = dbType.getConnectionString(this);\r
-                       mConn = DriverManager.getConnection(mUrl, getUsername(), getPassword());\r
+                       if(connection != null && connection.isValid(TIMEOUT)){\r
+                               return connection;\r
+                       }else{\r
+                               IDatabaseType dbType = getDatabaseType().getDatabaseType();\r
+                               String classString = dbType.getClassString();\r
+                               Class.forName(classString);\r
+                               String mUrl = dbType.getConnectionString(this);\r
+                               return DriverManager.getConnection(mUrl, getUsername(), getPassword()); \r
+                       }\r
                } catch (ClassNotFoundException e) {\r
                        logger.error("Database driver class could not be loaded\n" + "Exception: " + e.toString());\r
                } catch(SQLException e) {\r
                        logger.error("Problems with database connection\n" + "Exception: " + e.toString());\r
                }\r
-               return mConn;\r
+               return null;\r
        }\r
-\r
        \r
        /* (non-Javadoc)\r
         * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()\r
         */\r
-       public boolean testConnection() {\r
+       public boolean testConnection() throws ClassNotFoundException, SQLException {\r
 \r
-               try {\r
-                       Connection mConn = getConnection();\r
-                       if (mConn != null){\r
-                               return true;\r
-                       }else{\r
-                               return false;\r
-                       }\r
-               } catch (Exception e) {\r
-                       logger.warn(e.getMessage());\r
-                       return false;\r
+               IDatabaseType dbType = getDatabaseType().getDatabaseType();\r
+               String classString = dbType.getClassString();\r
+               Class.forName(classString);\r
+               String mUrl = dbType.getConnectionString(this);\r
+               Connection connection = DriverManager.getConnection(mUrl, getUsername(), getPassword());\r
+               if (connection != null){\r
+                       return true;\r
                }\r
+               \r
+               return false;\r
        }\r
 \r
+       @Override\r
+       public Object getSingleValue(String query) throws SQLException{\r
+               String queryString = query == null? "(null)": query;  \r
+               ResultSet resultSet = executeQuery(query);\r
+               if (resultSet == null || resultSet.next() == false){\r
+                       logger.warn("No record returned for query " +  queryString);\r
+                       return null;\r
+               }\r
+               if (resultSet.getMetaData().getColumnCount() != 1){\r
+                       logger.warn("More than one column selected in query" +  queryString);\r
+                       return null;\r
+               }\r
+               Object object = resultSet.getObject(1);\r
+               if (resultSet.next()){\r
+                       logger.warn("Multiple results for query " +  queryString);\r
+                       return null;\r
+               }\r
+               return object;\r
+       }\r
+       \r
        \r
     /**\r
      * Executes a query and returns the ResultSet.\r
      * @return ResultSet for the query.\r
+     * @throws SQLException \r
      */\r
-       public ResultSet executeQuery (String query) {\r
+       @Override\r
+       public ResultSet executeQuery (String query) throws SQLException {\r
 \r
-               ResultSet rs;\r
-               try {\r
-                       if (query == null){\r
-                               return null;\r
-                       }\r
-                       Connection mConn = getConnection();\r
-                       Statement mStmt = mConn.createStatement();\r
-                       rs = mStmt.executeQuery(query);\r
-                       return rs;\r
-               } catch(SQLException e) {\r
-                       logger.error("Problems when executing query \n  " + query + " \n" + "Exception: " + e);\r
+               ResultSet resultSet;\r
+\r
+               if (query == null){\r
                        return null;\r
                }\r
+               Connection connection = getConnection();\r
+               if (connection != null){\r
+                       Statement statement = connection.createStatement();\r
+                       resultSet = statement.executeQuery(query);\r
+               }else{\r
+                       throw new RuntimeException("Could not establish connection to database");\r
+               }\r
+               return resultSet;\r
+\r
        }\r
        \r
     /**\r
      * Executes an update\r
      * @return return code\r
      */\r
+       @Override\r
        public int executeUpdate (String sqlUpdate) {\r
                \r
                int result;\r
+               Connection connection = null;\r
                try {\r
                        if (sqlUpdate == null){\r
                                return 0;\r
                        }\r
-                       Connection mConn = getConnection();\r
-                       Statement mStmt = mConn.createStatement();\r
-                       result = mStmt.executeUpdate(sqlUpdate);\r
+                       connection = getConnection();\r
+                       Statement statement = connection.createStatement();\r
+                       result = statement.executeUpdate(sqlUpdate);\r
                        return result;\r
                } catch(SQLException e) {\r
                        logger.error("Problems when executing update\n  " + sqlUpdate + " \n" + "Exception: " + e);\r
                        return 0;\r
                }\r
        }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#getMetaData()\r
+        */\r
+       @Override\r
+       public DatabaseMetaData getMetaData() {\r
+               Connection connection = null;\r
+               try {\r
+                       connection = getConnection();\r
+                       return connection.getMetaData();\r
+               } catch (SQLException e) {\r
+                       logger.error("Could not get metadata for datasource", e);\r
+                       return null;\r
+               }\r
+       }\r
+       \r
+       /* (non-Javadoc)\r
+        * @see eu.etaxonomy.cdm.database.ICdmDataSource#closeOpenConnections()\r
+        */\r
+       @Override\r
+       public void closeOpenConnections() {\r
+               try {\r
+                       if(connection != null && !connection.isClosed()){\r
+                               connection.close();\r
+                               connection = null;\r
+                       }\r
+               } catch (SQLException e) {\r
+                       logger.error("Error closing the connection");\r
+               }\r
+       }\r
 }\r