Skip to content

Instantly share code, notes, and snippets.

@trevershick
Created July 2, 2011 20:35
Show Gist options
  • Save trevershick/1061619 to your computer and use it in GitHub Desktop.
Save trevershick/1061619 to your computer and use it in GitHub Desktop.
DataSource wrapper that executes statements on getConnection to fix an issue with Activiti not supporting fully qualified table names.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<jee:jndi-lookup id="rawDataSource" jndi-name="jdbc/LOAORACLEDB"
resource-ref="true" />
<jee:jndi-lookup id="oracleSchema" jndi-name="env/oracleSchema"
resource-ref="true" />
<!-- THIS SHOULD ONLY BE IN THE WEBAPP. WE ONLY WANT TO FORCE IT TO THE
SCHEMA IF ITS RUNNING IN CONTAINER, OTHERWISE ITS RUNNING IN A UNIT TEST
AND THE UNIT TEST SHOULD SETUP THE APPROPRIATE CONNECTION -->
<bean id="dataSource" class="com.railinc.spring.jdbc.StatementExecutingDataSource">
<property name="dataSource" ref="rawDataSource" />
<property name="statements">
<util:list>
<value>ALTER SESSION SET CURRENT_SCHEMA=#{oracleSchema}</value>
</util:list>
</property>
</bean>
</beans>
package com.railinc.spring.jdbc;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.util.Assert;
import com.railinc.activiti.sso.RailincDeploymentManager;
/**
* Executes a list of SQL statements upon getConnection being called on the DataSource.
* this is very useful for forcing the current schema to be a certain value.
* for example, you could set your statements to include
* ALTER SESSION SET CURRENT_SCHEMA=loa
*
* @author sdtxs01
*
*/
public class StatementExecutingDataSource implements DataSource,
InitializingBean, BeanNameAware {
private final Logger log = LoggerFactory.getLogger(RailincDeploymentManager.class);
private DataSource dataSource;
private String beanName;
private List<String> statements;
public synchronized void setStatements(List<String> statements) {
if (log.isDebugEnabled()) {
log.debug("Statements set to " + statements + " on " + this.beanName);
}
if (statements != null && statements.size() > 0) {
this.statements = new ArrayList<String>(statements);
}
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public Connection getConnection() throws SQLException {
Connection c = dataSource.getConnection();
executeStatements(c);
return c;
}
private void executeStatements(Connection c) throws SQLException {
List<String> s = statements;
if (s == null) return;
if (log.isDebugEnabled()) {
log.debug("Executing statements " + s);
}
Statement statement = c.createStatement();
try {
for (String stmt : s) {
statement.execute(stmt);
}
} finally {
JdbcUtils.closeStatement(statement);
}
}
public Connection getConnection(String username, String password)
throws SQLException {
Connection c = dataSource.getConnection(username, password);
executeStatements(c);
return c;
}
public PrintWriter getLogWriter() throws SQLException {
return dataSource.getLogWriter();
}
public int getLoginTimeout() throws SQLException {
return dataSource.getLoginTimeout();
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return dataSource.isWrapperFor(iface);
}
public void setLogWriter(PrintWriter out) throws SQLException {
dataSource.setLogWriter(out);
}
public void setLoginTimeout(int seconds) throws SQLException {
dataSource.setLoginTimeout(seconds);
}
public <T> T unwrap(Class<T> iface) throws SQLException {
return dataSource.unwrap(iface);
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.dataSource, "dataSource must be set on bean " + this.beanName + " of type " + getClass().getName());
}
@Override
public void setBeanName(String name) {
this.beanName = name;
}
}
@dshadgitsystems
Copy link

make it abstract

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment