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 org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.database
.types
.IDatabaseType
;
28 abstract class CdmDataSourceBase
implements ICdmDataSource
{
29 private static final Logger logger
= Logger
.getLogger(CdmDataSourceBase
.class);
31 private static final int TIMEOUT
= 10;
32 private Connection connection
;
35 public Connection
getConnection() throws SQLException
{
36 return getConnection(getUsername(), getPassword());
40 public Connection
getConnection(String username
, String password
) throws SQLException
{
42 if(connection
!= null){
43 boolean isValid
= true;
45 // isValid = connection.isValid(TIMEOUT);
46 // } catch (java.lang.AbstractMethodError e){
47 // logger.error("Problems with Connection.isValid method\n" + "Exception: " + e.toString());
53 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
54 String classString
= dbType
.getClassString();
55 Class
.forName(classString
);
56 String mUrl
= dbType
.getConnectionString(this);
57 Connection connection
= DriverManager
.getConnection(mUrl
, username
, password
);
60 } catch (ClassNotFoundException e
) {
61 logger
.error("Database driver class could not be loaded\n" + "Exception: " + e
.toString());
62 } catch(SQLException e
) {
63 logger
.error("Problems with database connection\n" + "Exception: " + e
.toString());
70 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
72 public boolean testConnection() throws ClassNotFoundException
, SQLException
{
74 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
75 String classString
= dbType
.getClassString();
76 Class
.forName(classString
);
77 String mUrl
= dbType
.getConnectionString(this);
78 Connection connection
= DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
79 if (connection
!= null){
87 public Object
getSingleValue(String query
) throws SQLException
{
88 String queryString
= query
== null?
"(null)": query
;
89 ResultSet resultSet
= executeQuery(query
);
90 if (resultSet
== null || resultSet
.next() == false){
91 logger
.info("No record returned for query " + queryString
);
94 if (resultSet
.getMetaData().getColumnCount() != 1){
95 logger
.info("More than one column selected in query" + queryString
);
96 //first value will be taken
98 Object object
= resultSet
.getObject(1);
99 if (resultSet
.next()){
100 logger
.info("Multiple results for query " + queryString
);
101 //first row will be taken
108 * Executes a query and returns the ResultSet.
109 * @return ResultSet for the query.
110 * @throws SQLException
113 public ResultSet
executeQuery (String query
) throws SQLException
{
120 Connection connection
= getConnection();
121 if (connection
!= null){
122 Statement statement
= connection
.createStatement();
123 resultSet
= statement
.executeQuery(query
);
125 throw new RuntimeException("Could not establish connection to database");
133 * @see eu.etaxonomy.cdm.database.ICdmDataSource#executeUpdate(java.lang.String)
136 public int executeUpdate (String sqlUpdate
) throws SQLException
{
139 Connection connection
= null;
141 if (sqlUpdate
== null){
144 connection
= getConnection();
145 Statement statement
= connection
.createStatement();
146 result
= statement
.executeUpdate(sqlUpdate
);
148 } catch(SQLException e
) {
150 if (! connection
.getAutoCommit()){
151 connection
.rollback();
153 }catch (SQLException ex
){
154 //do nothing - maybe throw RuntimeException in future
155 throw new RuntimeException(ex
);
157 logger
.error("Problems when executing update\n " + sqlUpdate
+ " \n" + "Exception: " + e
);
164 * @see eu.etaxonomy.cdm.database.ICdmDataSource#startTransaction()
167 public void startTransaction() {
169 Connection connection
= getConnection();
170 this.connection
= connection
;
171 connection
.setAutoCommit(false);
173 } catch(SQLException e
) {
174 logger
.error("Problems when starting transaction \n" + "Exception: " + e
);
180 * @see eu.etaxonomy.cdm.database.ICdmDataSource#commitTransaction()
183 public void commitTransaction() throws SQLException
{
185 Connection connection
= getConnection();
187 } catch(SQLException e
) {
188 logger
.error("Problems when commiting transaction \n" + "Exception: " + e
);
194 public void rollback() throws SQLException
{
196 Connection connection
= getConnection();
197 connection
.rollback();
198 } catch(SQLException e
) {
199 logger
.error("Problems when rolling back transaction \n" + "Exception: " + e
);
205 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getMetaData()
208 public DatabaseMetaData
getMetaData() {
209 Connection connection
= null;
211 connection
= getConnection();
212 return connection
.getMetaData();
213 } catch (SQLException e
) {
214 logger
.error("Could not get metadata for datasource", e
);
220 * @see eu.etaxonomy.cdm.database.ICdmDataSource#closeOpenConnections()
223 public void closeOpenConnections() {
225 if(connection
!= null && !connection
.isClosed()){
229 } catch (SQLException e
) {
230 logger
.error("Error closing the connection");