* 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
+import java.sql.Statement;\r
+\r
+import javax.sql.DataSource;\r
\r
import org.apache.log4j.Logger;\r
\r
/**\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
+\r
+ public Connection getConnection() throws SQLException {\r
+ return getConnection(getUsername(), getPassword());\r
+ }\r
\r
\r
+ public Connection getConnection(String username, String password) throws SQLException {\r
+ try {\r
+ if(connection != null){\r
+ boolean isValid = true;\r
+// try{\r
+// isValid = connection.isValid(TIMEOUT);\r
+// } catch (java.lang.AbstractMethodError e){\r
+// logger.error("Problems with Connection.isValid method\n" + "Exception: " + e.toString());\r
+// }\r
+ if (isValid){\r
+ return connection;\r
+ }\r
+ }else{\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, username, password);\r
+ return connection;\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 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
+ 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
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.database.ICdmDataSource#getSingleValue(java.lang.String)\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
+ @Override\r
+ public ResultSet executeQuery (String query) throws SQLException {\r
+\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
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.database.ICdmDataSource#executeUpdate(java.lang.String)\r
+ */\r
+ @Override\r
+ public int executeUpdate (String sqlUpdate) throws SQLException{\r
+ \r
+ int result;\r
+ Connection connection = null;\r
try {\r
- IDatabaseType dbType = getDatabaseType().getDatabaseType();\r
- String classString = dbType.getClassString();\r
- Class.forName(classString);\r
- \r
- String mUrl = dbType.getConnectionString(this);\r
- Connection mConn = DriverManager.getConnection(mUrl, getUserName(), getPassword());\r
- if (mConn != null){\r
- return true;\r
- }else{\r
- return false;\r
+ if (sqlUpdate == null){\r
+ return 0;\r
+ }\r
+ connection = getConnection();\r
+ Statement statement = connection.createStatement();\r
+ result = statement.executeUpdate(sqlUpdate);\r
+ return result;\r
+ } catch(SQLException e) {\r
+ try{\r
+ if (! connection.getAutoCommit()){\r
+ connection.rollback();\r
+ }\r
+ }catch (SQLException ex){\r
+ //do nothing - maybe throw RuntimeException in future\r
+ throw new RuntimeException(ex);\r
+ }\r
+ logger.error("Problems when executing update\n " + sqlUpdate + " \n" + "Exception: " + e);\r
+ throw e;\r
+ }\r
+ }\r
+ \r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.database.ICdmDataSource#startTransaction()\r
+ */\r
+ @Override\r
+ public void startTransaction() {\r
+ try {\r
+ Connection connection = getConnection();\r
+ connection.setAutoCommit(false);\r
+ this.connection = connection;\r
+ return;\r
+ } catch(SQLException e) {\r
+ logger.error("Problems when starting transaction \n" + "Exception: " + e);\r
+ return;\r
+ }\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.database.ICdmDataSource#commitTransaction()\r
+ */\r
+ @Override\r
+ public void commitTransaction() throws SQLException {\r
+ try {\r
+ Connection connection = getConnection();\r
+ connection.commit();\r
+ } catch(SQLException e) {\r
+ logger.error("Problems when commiting transaction \n" + "Exception: " + e);\r
+ throw e;\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void rollback() throws SQLException {\r
+ try {\r
+ Connection connection = getConnection();\r
+ connection.rollback();\r
+ } catch(SQLException e) {\r
+ logger.error("Problems when rolling back transaction \n" + "Exception: " + e);\r
+ throw e;\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 (Exception e) {\r
- logger.warn(e.getMessage());\r
- return false;\r
+ } catch (SQLException e) {\r
+ logger.error("Error closing the connection");\r
}\r
}\r
}\r