2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License VeresultSetion 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
;
12 import java
.sql
.Connection
;
13 import java
.sql
.DatabaseMetaData
;
14 import java
.sql
.DriverManager
;
15 import java
.sql
.ResultSet
;
16 import java
.sql
.SQLException
;
17 import java
.sql
.Statement
;
19 import javax
.sql
.DataSource
;
21 import org
.apache
.log4j
.Logger
;
23 import eu
.etaxonomy
.cdm
.database
.types
.IDatabaseType
;
30 abstract class CdmDataSourceBase
implements ICdmDataSource
{
31 private static final Logger logger
= Logger
.getLogger(CdmDataSourceBase
.class);
33 private static final int TIMEOUT
= 10;
34 private Connection connection
;
37 public Connection
getConnection() throws SQLException
{
38 return getConnection(getUsername(), getPassword());
42 public Connection
getConnection(String username
, String password
) throws SQLException
{
44 if(connection
!= null){
45 boolean isValid
= true;
47 // isValid = connection.isValid(TIMEOUT);
48 // } catch (java.lang.AbstractMethodError e){
49 // logger.error("Problems with Connection.isValid method\n" + "Exception: " + e.toString());
55 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
56 String classString
= dbType
.getClassString();
57 Class
.forName(classString
);
58 String mUrl
= dbType
.getConnectionString(this);
59 Connection connection
= DriverManager
.getConnection(mUrl
, username
, password
);
62 } catch (ClassNotFoundException e
) {
63 logger
.error("Database driver class could not be loaded\n" + "Exception: " + e
.toString());
64 } catch(SQLException e
) {
65 logger
.error("Problems with database connection\n" + "Exception: " + e
.toString());
72 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
74 public boolean testConnection() throws ClassNotFoundException
, SQLException
{
76 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
77 String classString
= dbType
.getClassString();
78 Class
.forName(classString
);
79 String mUrl
= dbType
.getConnectionString(this);
80 Connection connection
= DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
81 if (connection
!= null){
89 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getSingleValue(java.lang.String)
92 public Object
getSingleValue(String query
) throws SQLException
{
93 String queryString
= query
== null?
"(null)": query
;
94 ResultSet resultSet
= executeQuery(query
);
95 if (resultSet
== null || resultSet
.next() == false){
96 logger
.warn("No record returned for query " + queryString
);
99 if (resultSet
.getMetaData().getColumnCount() != 1){
100 logger
.warn("More than one column selected in query" + queryString
);
103 Object object
= resultSet
.getObject(1);
104 if (resultSet
.next()){
105 logger
.warn("Multiple results for query " + queryString
);
113 * Executes a query and returns the ResultSet.
114 * @return ResultSet for the query.
115 * @throws SQLException
118 public ResultSet
executeQuery (String query
) throws SQLException
{
125 Connection connection
= getConnection();
126 if (connection
!= null){
127 Statement statement
= connection
.createStatement();
128 resultSet
= statement
.executeQuery(query
);
130 throw new RuntimeException("Could not establish connection to database");
138 * @see eu.etaxonomy.cdm.database.ICdmDataSource#executeUpdate(java.lang.String)
141 public int executeUpdate (String sqlUpdate
) throws SQLException
{
144 Connection connection
= null;
146 if (sqlUpdate
== null){
149 connection
= getConnection();
150 Statement statement
= connection
.createStatement();
151 result
= statement
.executeUpdate(sqlUpdate
);
153 } catch(SQLException e
) {
155 if (! connection
.getAutoCommit()){
156 connection
.rollback();
158 }catch (SQLException ex
){
159 //do nothing - maybe throw RuntimeException in future
160 throw new RuntimeException(ex
);
162 logger
.error("Problems when executing update\n " + sqlUpdate
+ " \n" + "Exception: " + e
);
169 * @see eu.etaxonomy.cdm.database.ICdmDataSource#startTransaction()
172 public void startTransaction() {
174 Connection connection
= getConnection();
175 connection
.setAutoCommit(false);
176 this.connection
= connection
;
178 } catch(SQLException e
) {
179 logger
.error("Problems when starting transaction \n" + "Exception: " + e
);
185 * @see eu.etaxonomy.cdm.database.ICdmDataSource#commitTransaction()
188 public void commitTransaction() throws SQLException
{
190 Connection connection
= getConnection();
192 } catch(SQLException e
) {
193 logger
.error("Problems when commiting transaction \n" + "Exception: " + e
);
199 public void rollback() throws SQLException
{
201 Connection connection
= getConnection();
202 connection
.rollback();
203 } catch(SQLException e
) {
204 logger
.error("Problems when rolling back transaction \n" + "Exception: " + e
);
210 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getMetaData()
213 public DatabaseMetaData
getMetaData() {
214 Connection connection
= null;
216 connection
= getConnection();
217 return connection
.getMetaData();
218 } catch (SQLException e
) {
219 logger
.error("Could not get metadata for datasource", e
);
225 * @see eu.etaxonomy.cdm.database.ICdmDataSource#closeOpenConnections()
228 public void closeOpenConnections() {
230 if(connection
!= null && !connection
.isClosed()){
234 } catch (SQLException e
) {
235 logger
.error("Error closing the connection");