001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.dbunit.util;
017:
018: import org.dbunit.database.AbstractDatabaseConnection;
019: import org.unitils.database.transaction.TransactionalDataSource;
020:
021: import java.sql.Connection;
022: import java.sql.SQLException;
023:
024: /**
025: * Implementation of DBUnits <code>IDatabaseConnection</code> interface. This implementation returns connections from
026: * an underlying <code>DataSource</code>. This implementation stores the <code>Connection</code> that was retrieved last,
027: * to enable closing it (or returing it to the pool) using {@link #closeJdbcConnection()}.
028: *
029: * @author Filip Neven
030: * @author Tim Ducheyne
031: */
032: public class DbUnitDatabaseConnection extends
033: AbstractDatabaseConnection {
034:
035: /* DataSource that provides access to JDBC connections */
036: private TransactionalDataSource dataSource;
037:
038: /* Name of the database schema */
039: private String schemaName;
040:
041: /* Connection that is currently in use by DBUnit. Is stored to enable returning it to the connection pool after
042: the DBUnit operation finished */
043: private Connection currentlyUsedConnection;
044:
045: /**
046: * Creates a new instance that wraps the given <code>DataSource</code>
047: *
048: * @param dataSource The data source, not null
049: * @param schemaName The database schema, not null
050: */
051: public DbUnitDatabaseConnection(TransactionalDataSource dataSource,
052: String schemaName) {
053: this .dataSource = dataSource;
054: this .schemaName = schemaName;
055: }
056:
057: /**
058: * Method that is invoked by DBUnit when the connection is no longer needed. This method is not implemented,
059: * connections are 'closed' (returned to the connection pool) after every DBUnit operation
060: */
061: @Override
062: public void close() throws SQLException {
063: // Nothing to be done. Connections are closed (i.e. returned to the pool) after every dbUnit operation
064: }
065:
066: /**
067: * @return The database schema name
068: */
069: @Override
070: public String getSchema() {
071: return schemaName;
072: }
073:
074: /**
075: * Returns a <code>Connection</code> that can be used by DBUnit. A reference to the connection is kept, to be able
076: * to 'close' it (return it to the connection pool) after the DBUnit operation finished. If an open connection
077: * is already in use by DBUnit, this connection is returned
078: *
079: * @return A JDBC connection
080: */
081: @Override
082: public Connection getConnection() throws SQLException {
083: if (currentlyUsedConnection == null) {
084: currentlyUsedConnection = dataSource
085: .getTransactionalConnection();
086: }
087: return currentlyUsedConnection;
088: }
089:
090: /**
091: * Closes the <code>Connection</code> that was last retrieved using the {@link #getConnection} method
092: *
093: * @throws SQLException When connection close fails
094: */
095: public void closeJdbcConnection() throws SQLException {
096: if (currentlyUsedConnection != null) {
097: currentlyUsedConnection.close();
098: currentlyUsedConnection = null;
099: }
100: }
101:
102: }
|