01: /*
02: * Copyright 2000-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.jetspeed.components.datasource;
17:
18: import java.sql.Connection;
19: import java.sql.SQLException;
20: import java.sql.Statement;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24: import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
25:
26: /**
27: * The SchemaAwareDataSourceProxy optionally injects a schema selection into an
28: * existing database connection. It proxies a DataSource and executes an
29: * injected sql statement on every getConnection() call.
30: *
31: * Inspired by http://forum.springframework.org/showthread.php?t=10728, runtime
32: * schema switching was stripped.
33: *
34: * @author <a href="mailto:joachim@wemove.com">Joachim Müller</a>
35: * @version $Id: SchemaAwareDataSourceProxy.java 601297 2007-12-05 11:20:04Z ate $
36: */
37: public class SchemaAwareDataSourceProxy extends
38: TransactionAwareDataSourceProxy {
39: private static final Log log = LogFactory
40: .getLog(SchemaAwareDataSourceProxy.class);
41:
42: private String schemaSql = null;
43:
44: public void setSchemaSql(String schemaSql) {
45: this .schemaSql = schemaSql;
46: }
47:
48: public Connection getConnection() throws SQLException {
49: Connection con = super .getConnection();
50:
51: if (schemaSql != null) {
52: if (log.isDebugEnabled()) {
53: log.debug("Setting schema by executing sql '"
54: + schemaSql + "' on connection " + con);
55: }
56:
57: Statement stmt = con.createStatement();
58: try {
59: // database specific SQL.
60: stmt.execute(schemaSql);
61: } catch (Exception e) {
62: log.error("Error executing table schema setting sql: '"
63: + schemaSql + "'.", e);
64: } finally {
65: stmt.close();
66: }
67: }
68:
69: return con;
70: }
71: }
|