Project

General

Profile

Download (11.9 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.io.PrintWriter;
13
import java.sql.Connection;
14
import java.sql.DatabaseMetaData;
15
import java.sql.DriverManager;
16
import java.sql.ResultSet;
17
import java.sql.SQLException;
18
import java.sql.Statement;
19
import java.util.HashMap;
20
import java.util.Map;
21

    
22
import org.apache.log4j.Logger;
23

    
24
import eu.etaxonomy.cdm.config.CdmSource;
25
import eu.etaxonomy.cdm.config.CdmSourceException;
26
import eu.etaxonomy.cdm.database.types.IDatabaseType;
27
import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
28

    
29
/**
30
 * @author a.mueller
31
 * @created 18.12.2008
32
 * @veresultSetion 1.0
33
 */
34
abstract class CdmDataSourceBase extends CdmSource implements ICdmDataSource  {
35

    
36
    private static final Logger logger = Logger.getLogger(CdmDataSourceBase.class);
37

    
38

    
39
    //	private static final int TIMEOUT = 10;
40
    private Connection connection;
41

    
42

    
43
    @Override
44
    public Connection getConnection() throws SQLException {
45
        return getConnection(getUsername(), getPassword());
46
    }
47

    
48

    
49
    @Override
50
    public Connection getConnection(String username, String password) throws SQLException {
51
        try {
52
            if(connection != null){
53
                boolean isValid = true;
54
//				try{
55
//					isValid = connection.isValid(TIMEOUT);
56
//				} catch (java.lang.AbstractMethodError e){
57
//					logger.error("Problems with Connection.isValid method\n" + "Exception: " + e.toString());
58
//				}
59
                if (isValid){
60
                    return connection;
61
                }
62
            }else{
63
                IDatabaseType dbType = getDatabaseType().getDatabaseType();
64
                String classString = dbType.getClassString();
65
                Class.forName(classString);
66
                String mUrl = dbType.getConnectionString(this);
67
                Connection connection = DriverManager.getConnection(mUrl, username, password);
68
                return 	connection;
69
            }
70
        } catch (ClassNotFoundException e) {
71
            throw new RuntimeException("Database driver class could not be loaded\n" + "Exception: " + e.toString(),e);
72
        } catch(SQLException e) {
73
            throw new RuntimeException("Problems with database connection\n" + "Exception: " + e.toString(), e);
74
        }
75
        return null;
76
    }
77

    
78
    @Override
79
    public boolean testConnection() throws ClassNotFoundException, SQLException {
80

    
81
        IDatabaseType dbType = getDatabaseType().getDatabaseType();
82
        String classString = dbType.getClassString();
83
        Class.forName(classString);
84
        String mUrl = dbType.getConnectionString(this);
85
        if(logger.isDebugEnabled()){
86
            logger.debug("testConnection() : " + mUrl);
87
        }
88

    
89
        if(logger.isDebugEnabled()){
90
            logger.debug("testConnection() : " + mUrl + " : service is available");
91
        }
92
        // try to connect to the database server
93
        Connection connection = DriverManager.getConnection(mUrl, getUsername(), getPassword());
94
        if (connection != null){
95
            if(logger.isDebugEnabled()){
96
                logger.debug("testConnection() : " + mUrl + " : jdbc connect successful");
97
            }
98
            return true;
99
        }
100

    
101
        if(logger.isDebugEnabled()){
102
            logger.debug("testConnection() : " + mUrl + " : FAIL");
103
        }
104
        return false;
105
    }
106

    
107
	@Override
108
	public boolean checkConnection() throws CdmSourceException {
109
		try {
110
			return testConnection();
111
		} catch (ClassNotFoundException e) {
112
			throw new CdmSourceException(e.getMessage());			
113
		} catch (SQLException e) {
114
			throw new CdmSourceException(e.getMessage());	
115
		}
116
	}
117
	
118
	@Override
119
	public String getConnectionMessage() {
120
		String message = "";
121
		if (getDatabaseType().equals(DatabaseTypeEnum.H2)) {
122
			message = " local CDM Store ";
123
		} else {
124
			message = " CDM Community Store ";
125
		}
126
		message += "'" + getName() + "'";
127

    
128
		message = "Connecting to" + message + ".";
129

    
130
		return message;
131
	}
132
	
133
    @Override
134
    public Object getSingleValue(String query) throws SQLException{
135
        String queryString = query == null? "(null)": query;
136
        ResultSet resultSet = executeQuery(query);
137
        if (resultSet == null || resultSet.next() == false){
138
            logger.info("No record returned for query " +  queryString);
139
            return null;
140
        }
141
        if (resultSet.getMetaData().getColumnCount() != 1){
142
            logger.info("More than one column selected in query" +  queryString);
143
            //first value will be taken
144
        }
145
        Object object = resultSet.getObject(1);
146
        if (resultSet.next()){
147
            logger.info("Multiple results for query " +  queryString);
148
            //first row will be taken
149
        }
150
        return object;
151
    }
152

    
153
	@Override
154
	public  String getDbSchemaVersion() throws CdmSourceException  {		
155
		try {
156
			return (String)getSingleValue(MetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
157
		} catch (SQLException e) {
158
			throw new CdmSourceException(e.getMessage());	
159
		}
160
	}
161
	
162
	@Override
163
	public boolean isDbEmpty() throws CdmSourceException {
164
		// Any CDM DB should have a schema version
165
		String dbSchemaVersion = (String) getDbSchemaVersion();
166
		
167
		return (dbSchemaVersion == null || dbSchemaVersion.equals(""));
168
	}
169
    /**
170
     * Executes a query and returns the ResultSet.
171
     * @return ResultSet for the query.
172
     * @throws SQLException
173
     */
174
    @Override
175
    public ResultSet executeQuery (String query) throws SQLException {
176

    
177
        ResultSet resultSet;
178

    
179
        if (query == null){
180
            return null;
181
        }
182
        Connection connection = getConnection();
183
        if (connection != null){
184
            Statement statement = connection.createStatement();
185
            resultSet = statement.executeQuery(query);
186
        }else{
187
            throw new RuntimeException("Could not establish connection to database");
188
        }
189
        return resultSet;
190

    
191
    }
192

    
193
    @Override
194
    public int executeUpdate (String sqlUpdate) throws SQLException{
195

    
196
        int result;
197
        Connection connection = null;
198
        try {
199
            if (sqlUpdate == null){
200
                return 0;
201
            }
202
            connection = getConnection();
203
            Statement statement = connection.createStatement();
204
            result = statement.executeUpdate(sqlUpdate);
205
            return result;
206
        } catch(SQLException e) {
207
            try{
208
                if (connection != null && ! connection.getAutoCommit()){
209
                    connection.rollback();
210
                }
211
            }catch (SQLException ex){
212
                //do nothing -  maybe throw RuntimeException in future
213
                throw new RuntimeException(ex);
214
            }
215
            logger.error("Problems when executing update\n  " + sqlUpdate + " \n" + "Exception: " + e);
216
            throw e;
217
        }
218
    }
219

    
220
    @Override
221
    public void startTransaction() {
222
        try {
223
            Connection connection = getConnection();
224
            this.connection = connection;
225
            connection.setAutoCommit(false);
226
            return;
227
        } catch(SQLException e) {
228
            logger.error("Problems when starting transaction \n" + "Exception: " + e);
229
            return;
230
        }
231
    }
232

    
233
    @Override
234
    public void commitTransaction() throws SQLException {
235
        try {
236
            Connection connection = getConnection();
237
            connection.commit();
238
        } catch(SQLException e) {
239
            logger.error("Problems when commiting transaction \n" + "Exception: " + e);
240
            throw e;
241
        }
242
    }
243

    
244
    @Override
245
    public void rollback() throws SQLException {
246
        try {
247
            Connection connection = getConnection();
248
            connection.rollback();
249
        } catch(SQLException e) {
250
            logger.error("Problems when rolling back transaction \n" + "Exception: " + e);
251
            throw e;
252
        }
253
    }
254

    
255

    
256
    @Override
257
    public DatabaseMetaData getMetaData() {
258
        Connection connection = null;
259
        try {
260
            connection = getConnection();
261
            return connection.getMetaData();
262
        } catch (SQLException e) {
263
            logger.error("Could not get metadata for datasource", e);
264
            return null;
265
        }
266
    }
267

    
268
    @Override
269
    public void closeOpenConnections() {
270
        try {
271
            if(connection != null && !connection.isClosed()){
272
                connection.close();
273
                connection = null;
274
            }
275
        } catch (SQLException e) {
276
            logger.error("Error closing the connection");
277
        }
278
    }
279
    
280
    
281
    @Override
282
    public Map<MetaDataPropertyName, String> getMetaDataMap() throws CdmSourceException {
283
		Map<MetaDataPropertyName, String> cdmMetaDataMap = new HashMap<MetaDataPropertyName, String>();
284
		
285
		for(MetaDataPropertyName mdpn : MetaDataPropertyName.values()) {
286
			String value = null;
287
			try {
288
				value = (String)getSingleValue(mdpn.getSqlQuery());
289
			} catch (SQLException e) {
290
				throw new CdmSourceException(e.getMessage());
291
			}			
292
			if(value != null) {
293
				cdmMetaDataMap.put(mdpn, value);
294
			}		
295
		}
296
		return cdmMetaDataMap;
297
    }
298

    
299
    // ************ javax.sql.DataSource base interfaces ********************/
300

    
301

    
302
    @Override
303
    public PrintWriter getLogWriter() throws SQLException {
304
        //implementations copied from org.springframework.jdbc.datasource.AbstractDataSource;
305
        throw new UnsupportedOperationException("getLogWriter");
306
    }
307

    
308

    
309
    @Override
310
    public void setLogWriter(PrintWriter out) throws SQLException {
311
        //implementations copied from org.springframework.jdbc.datasource.AbstractDataSource;
312
        throw new UnsupportedOperationException("setLogWriter");
313
    }
314

    
315

    
316
    @Override
317
    public void setLoginTimeout(int seconds) throws SQLException {
318
        //implementations copied from org.springframework.jdbc.datasource.AbstractDataSource;
319
        throw new UnsupportedOperationException("setLoginTimeout");
320
    }
321

    
322

    
323
    @Override
324
    public int getLoginTimeout() throws SQLException {
325
        //implementations copied from org.springframework.jdbc.datasource.AbstractDataSource;
326
        return 0;
327
    }
328

    
329

    
330
    /*
331
     * This is a preliminary implementation to be compliant with
332
     * java.sql.Datasource (1.6). It may not be fully working.
333
     * Please let the developers know if this doesn't work.
334
     */
335

    
336
    //---------------------------------------------------------------------
337
    // Implementation of JDBC 4.0's Wrapper interface
338
    //---------------------------------------------------------------------
339

    
340
    @Override
341
    @SuppressWarnings("unchecked")
342
    public <T> T unwrap(Class<T> iface) throws SQLException {
343
        if (iface.isInstance(this)) {
344
            return (T) this;
345
        }
346
        throw new SQLException("DataSource of type [" + getClass().getName() +
347
                "] cannot be unwrapped as [" + iface.getName() + "]");
348
    }
349

    
350
    @Override
351
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
352
        return iface.isInstance(this);
353
    }
354

    
355

    
356
    //---------------------------------------------------------------------
357
    // Implementation of JDBC 4.1's getParentLogger method
358
    // Required in Java >=7.x
359
    // must not have the @Override annotation for compatibility with
360
    // java 1.6
361
    //---------------------------------------------------------------------
362

    
363
    public java.util.logging.Logger getParentLogger() {
364
        //copied from org.springframework.jdbc.datasource.AbstractDataSource, not checked if this is correct
365
        return java.util.logging.Logger.getLogger(java.util.logging.Logger.GLOBAL_LOGGER_NAME);
366
    }
367

    
368
 
369
}
(2-2/20)