1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.components.datasource;
1718import java.sql.Connection;
19import java.sql.SQLException;
20import java.sql.Statement;
2122import org.apache.commons.logging.Log;
23import org.apache.commons.logging.LogFactory;
24import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
2526/***27 * The SchemaAwareDataSourceProxy optionally injects a schema selection into an28 * existing database connection. It proxies a DataSource and executes an29 * injected sql statement on every getConnection() call.30 * 31 * Inspired by http://forum.springframework.org/showthread.php?t=10728, runtime32 * 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 */37publicclassSchemaAwareDataSourceProxyextends TransactionAwareDataSourceProxy
38 {
39privatestaticfinal Log log = LogFactory.getLog(SchemaAwareDataSourceProxy.class);
4041private String schemaSql = null;
4243publicvoid setSchemaSql(String schemaSql)
44 {
45this.schemaSql = schemaSql;
46 }
4748public Connection getConnection() throws SQLException
49 {
50 Connection con = super.getConnection();
5152if (schemaSql != null)
53 {
54if (log.isDebugEnabled())
55 {
56 log.debug("Setting schema by executing sql '" + schemaSql + "' on connection " + con);
57 }
5859 Statement stmt = con.createStatement();
60try61 {
62// database specific SQL.63 stmt.execute(schemaSql);
64 }
65catch (Exception e)
66 {
67 log.error("Error executing table schema setting sql: '" + schemaSql + "'.", e);
68 }
69finally70 {
71 stmt.close();
72 }
73 }
7475return con;
76 }
77 }