Project

General

Profile

Download (4.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9

    
10
package eu.etaxonomy.cdm.database;
11

    
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;
18

    
19
import org.apache.log4j.Logger;
20

    
21
import eu.etaxonomy.cdm.database.types.IDatabaseType;
22

    
23
/**
24
 * @author a.mueller
25
 * @created 18.12.2008
26
 * @veresultSetion 1.0
27
 */
28
abstract class CdmDataSourceBase implements ICdmDataSource {
29
	private static final Logger logger = Logger.getLogger(CdmDataSourceBase.class);
30

    
31
	private static final int TIMEOUT = 10;
32
	private Connection connection;
33
	
34
	private Connection getConnection() {
35

    
36
		try {
37
			if(connection != null && connection.isValid(TIMEOUT)){
38
				return connection;
39
			}else{
40
				IDatabaseType dbType = getDatabaseType().getDatabaseType();
41
				String classString = dbType.getClassString();
42
				Class.forName(classString);
43
				String mUrl = dbType.getConnectionString(this);
44
				return DriverManager.getConnection(mUrl, getUsername(), getPassword());	
45
			}
46
		} catch (ClassNotFoundException e) {
47
			logger.error("Database driver class could not be loaded\n" + "Exception: " + e.toString());
48
		} catch(SQLException e) {
49
			logger.error("Problems with database connection\n" + "Exception: " + e.toString());
50
		}
51
		return null;
52
	}
53
	
54
	/* (non-Javadoc)
55
	 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
56
	 */
57
	public boolean testConnection() throws DataSourceNotFoundException {
58

    
59
		IDatabaseType dbType = getDatabaseType().getDatabaseType();
60
		String classString = dbType.getClassString();
61
		try {
62
			Class.forName(classString);
63
			String mUrl = dbType.getConnectionString(this);
64
			Connection connection = DriverManager.getConnection(mUrl, getUsername(), getPassword());
65
			if (connection != null){
66
				return true;
67
			}
68
		} catch (ClassNotFoundException e) {
69
			throw new DataSourceNotFoundException(e);
70
		} catch (SQLException e) {
71
			throw new DataSourceNotFoundException(e);
72
		}
73
		return false;
74
	}
75

    
76
	@Override
77
	public Object getSingleValue(String query) throws SQLException{
78
		String queryString = query == null? "(null)": query;  
79
		ResultSet resultSet = executeQuery(query);
80
		if (resultSet == null || resultSet.next() == false){
81
			logger.warn("No record returned for query " +  queryString);
82
			return null;
83
		}
84
		if (resultSet.getMetaData().getColumnCount() != 1){
85
			logger.warn("More than one column selected in query" +  queryString);
86
			return null;
87
		}
88
		Object object = resultSet.getObject(1);
89
		if (resultSet.next()){
90
			logger.warn("Multiple results for query " +  queryString);
91
			return null;
92
		}
93
		return object;
94
	}
95
	
96
	
97
    /**
98
     * Executes a query and returns the ResultSet.
99
     * @return ResultSet for the query.
100
     */
101
	@Override
102
	public ResultSet executeQuery (String query) {
103

    
104
		ResultSet resultSet;
105
		try {
106
			if (query == null){
107
				return null;
108
			}
109
			Connection connection = getConnection();
110
			Statement statement = connection.createStatement();
111
			resultSet = statement.executeQuery(query);
112
			return resultSet;
113
		}catch(SQLException e) {
114
			logger.error("Problems when executing query \n  " + query, e);
115
			return null;
116
		}
117
	}
118
	
119
    /**
120
     * Executes an update
121
     * @return return code
122
     */
123
	@Override
124
	public int executeUpdate (String sqlUpdate) {
125
		
126
		int result;
127
		Connection connection = null;
128
		try {
129
			if (sqlUpdate == null){
130
				return 0;
131
			}
132
			connection = getConnection();
133
			Statement statement = connection.createStatement();
134
			result = statement.executeUpdate(sqlUpdate);
135
			return result;
136
		} catch(SQLException e) {
137
			logger.error("Problems when executing update\n  " + sqlUpdate + " \n" + "Exception: " + e);
138
			return 0;
139
		}
140
	}
141
	
142
	/* (non-Javadoc)
143
	 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getMetaData()
144
	 */
145
	@Override
146
	public DatabaseMetaData getMetaData() {
147
		Connection connection = null;
148
		try {
149
			connection = getConnection();
150
			return connection.getMetaData();
151
		} catch (SQLException e) {
152
			logger.error("Could not get metadata for datasource", e);
153
			return null;
154
		}
155
	}
156
	
157
	/* (non-Javadoc)
158
	 * @see eu.etaxonomy.cdm.database.ICdmDataSource#closeOpenConnections()
159
	 */
160
	@Override
161
	public void closeOpenConnections() {
162
		try {
163
			if(connection != null && !connection.isClosed()){
164
				connection.close();
165
				connection = null;
166
			}
167
		} catch (SQLException e) {
168
			logger.error("Error closing the connection");
169
		}
170
	}
171
}
(2-2/18)