I have developed a solution for this a while ago (but gave up on DBUnit as it was too slow for unit tests I just have the db in a known state). But it should still work...
Code:
DataSource ds = (DataSource) ctx.getBean(BEAN_TEST_LOCAL_DS);
DatabaseDataSourceConnection jdbcConnection = new SpringDatabaseDataSourceConnection(ds);
jdbcConnection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new OracleDataTypeFactory());
Code:
package com.xyz.dbunit;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.dbunit.database.DatabaseDataSourceConnection;
import org.springframework.jdbc.datasource.DataSourceUtils;
/**
* Wrapped version of DBUnits DatabaseDataSourceConnection to enable Spring Transaction support.
*/
public class SpringDatabaseDataSourceConnection extends DatabaseDataSourceConnection {
private DataSource dataSource;
/**
* @param dataSource
* @throws SQLException
*/
public SpringDatabaseDataSourceConnection(DataSource dataSource) throws SQLException {
super(dataSource);
this.dataSource = dataSource;
}
/**
* @see org.dbunit.database.IDatabaseConnection#getConnection()
*/
public Connection getConnection() throws SQLException {
Connection conn = DataSourceUtils.getConnection(dataSource);
return new SpringConnection(dataSource, conn);
}
}
Code:
package com.xyz.dbunit;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DataSourceUtils;
/**
* Wrapped Connection enable Spring Transaction support.
*/
public class SpringConnection implements Connection {
private DataSource dataSource;
private Connection conn;
/**
* @param dataSource
* @param conn
* @throws SQLException
*/
public SpringConnection(DataSource dataSource, Connection conn) {
this.dataSource = dataSource;
this.conn = conn;
}
/**
* calls DataSourceUtils.closeConnectionIfNecessary rather than directly closing the connection
* @throws java.sql.SQLException
*/
public void close() throws SQLException {
DataSourceUtils.closeConnectionIfNecessary(conn, dataSource);
}
/**
* @throws java.sql.SQLException
*/
public void clearWarnings() throws SQLException {
conn.clearWarnings();
}
/**
* @throws java.sql.SQLException
*/
public void commit() throws SQLException {
//conn.commit();
}
/**
* @return
* @throws java.sql.SQLException
*/
public Statement createStatement() throws SQLException {
return conn.createStatement();
}
/**
* @param resultSetType
* @param resultSetConcurrency
* @return
* @throws java.sql.SQLException
*/
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* @param resultSetType
* @param resultSetConcurrency
* @param resultSetHoldability
* @return
* @throws java.sql.SQLException
*/
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
return conn.equals(obj);
}
/**
* @return
* @throws java.sql.SQLException
*/
public boolean getAutoCommit() throws SQLException {
return conn.getAutoCommit();
}
/**
* @return
* @throws java.sql.SQLException
*/
public String getCatalog() throws SQLException {
return conn.getCatalog();
}
/**
* @return
* @throws java.sql.SQLException
*/
public int getHoldability() throws SQLException {
return conn.getHoldability();
}
/**
* @return
* @throws java.sql.SQLException
*/
public DatabaseMetaData getMetaData() throws SQLException {
return conn.getMetaData();
}
/**
* @return
* @throws java.sql.SQLException
*/
public int getTransactionIsolation() throws SQLException {
return conn.getTransactionIsolation();
}
/**
* @return
* @throws java.sql.SQLException
*/
public Map getTypeMap() throws SQLException {
return conn.getTypeMap();
}
/**
* @return
* @throws java.sql.SQLException
*/
public SQLWarning getWarnings() throws SQLException {
return conn.getWarnings();
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return conn.hashCode();
}
/**
* @return
* @throws java.sql.SQLException
*/
public boolean isClosed() throws SQLException {
return conn.isClosed();
}
/**
* @return
* @throws java.sql.SQLException
*/
public boolean isReadOnly() throws SQLException {
return conn.isReadOnly();
}
/**
* @param sql
* @return
* @throws java.sql.SQLException
*/
public String nativeSQL(String sql) throws SQLException {
return conn.nativeSQL(sql);
}
/**
* @param sql
* @return
* @throws java.sql.SQLException
*/
public CallableStatement prepareCall(String sql) throws SQLException {
return conn.prepareCall(sql);
}
/**
* @param sql
* @param resultSetType
* @param resultSetConcurrency
* @return
* @throws java.sql.SQLException
*/
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return conn.prepareCall(sql, resultSetType, resultSetConcurrency);
}
/**
* @param sql
* @param resultSetType
* @param resultSetConcurrency
* @param resultSetHoldability
* @return
* @throws java.sql.SQLException
*/
public CallableStatement prepareCall(
String sql,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
throws SQLException {
return conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
/**
* @param sql
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
}
/**
* @param sql
* @param autoGeneratedKeys
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
return conn.prepareStatement(sql, autoGeneratedKeys);
}
/**
* @param sql
* @param resultSetType
* @param resultSetConcurrency
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/**
* @param sql
* @param resultSetType
* @param resultSetConcurrency
* @param resultSetHoldability
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(
String sql,
int resultSetType,
int resultSetConcurrency,
int resultSetHoldability)
throws SQLException {
return conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
/**
* @param sql
* @param columnIndexes
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {
return conn.prepareStatement(sql, columnIndexes);
}
/**
* @param sql
* @param columnNames
* @return
* @throws java.sql.SQLException
*/
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
return conn.prepareStatement(sql, columnNames);
}
/**
* @param savepoint
* @throws java.sql.SQLException
*/
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
conn.releaseSavepoint(savepoint);
}
/**
* @throws java.sql.SQLException
*/
public void rollback() throws SQLException {
conn.rollback();
}
/**
* @param savepoint
* @throws java.sql.SQLException
*/
public void rollback(Savepoint savepoint) throws SQLException {
//conn.rollback(savepoint);
}
/**
* @param autoCommit
* @throws java.sql.SQLException
*/
public void setAutoCommit(boolean autoCommit) throws SQLException {
conn.setAutoCommit(autoCommit);
}
/**
* @param catalog
* @throws java.sql.SQLException
*/
public void setCatalog(String catalog) throws SQLException {
conn.setCatalog(catalog);
}
/**
* @param holdability
* @throws java.sql.SQLException
*/
public void setHoldability(int holdability) throws SQLException {
conn.setHoldability(holdability);
}
/**
* @param readOnly
* @throws java.sql.SQLException
*/
public void setReadOnly(boolean readOnly) throws SQLException {
conn.setReadOnly(readOnly);
}
/**
* @return
* @throws java.sql.SQLException
*/
public Savepoint setSavepoint() throws SQLException {
return conn.setSavepoint();
}
/**
* @param name
* @return
* @throws java.sql.SQLException
*/
public Savepoint setSavepoint(String name) throws SQLException {
return conn.setSavepoint(name);
}
/**
* @param level
* @throws java.sql.SQLException
*/
public void setTransactionIsolation(int level) throws SQLException {
conn.setTransactionIsolation(level);
}
/**
* @param map
* @throws java.sql.SQLException
*/
public void setTypeMap(Map map) throws SQLException {
conn.setTypeMap(map);
}
/**
* @see java.lang.Object#toString()
*/
public String toString() {
return conn.toString();
}
}
Regards,
Gordon.
p.s. is it possible to add attachments to posts
:?: