DatabaseDataSourceConnection.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.database;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import javax.naming.InitialContext;
  25. import javax.naming.NamingException;
  26. import javax.sql.DataSource;
  27. import java.sql.Connection;
  28. import java.sql.SQLException;

  29. /**
  30.  * This class adapts a JDBC <code>DataSource</code> to a
  31.  * {@link IDatabaseConnection}.
  32.  *
  33.  * @author Manuel Laflamme
  34.  * @version $Revision$
  35.  * @since Mar 8, 2002
  36.  */
  37. public class DatabaseDataSourceConnection extends AbstractDatabaseConnection
  38.         implements IDatabaseConnection
  39. {

  40.     /**
  41.      * Logger for this class
  42.      */
  43.     private static final Logger logger = LoggerFactory.getLogger(DatabaseDataSourceConnection.class);

  44.     private final String _schema;
  45.     private final DataSource _dataSource;
  46.     private final String _user;
  47.     private final String _password;
  48.     private Connection _connection;

  49.     public DatabaseDataSourceConnection(InitialContext context, String jndiName,
  50.             String schema) throws NamingException, SQLException
  51.     {
  52.         this((DataSource)context.lookup(jndiName), schema, null, null);
  53.     }

  54.     public DatabaseDataSourceConnection(InitialContext context, String jndiName,
  55.             String schema, String user, String password)
  56.             throws NamingException, SQLException
  57.     {
  58.         this((DataSource)context.lookup(jndiName), schema, user, password);
  59.     }

  60.     public DatabaseDataSourceConnection(InitialContext context, String jndiName)
  61.             throws NamingException, SQLException
  62.     {
  63.         this(context, jndiName, null);
  64.     }

  65.     public DatabaseDataSourceConnection(InitialContext context, String jndiName,
  66.             String user, String password) throws NamingException, SQLException
  67.     {
  68.         this(context, jndiName, null, user, password);
  69.     }

  70.     public DatabaseDataSourceConnection(DataSource dataSource)
  71.             throws SQLException
  72.     {
  73.         this(dataSource, null, null, null);
  74.     }

  75.     public DatabaseDataSourceConnection(DataSource dataSource, String user,
  76.             String password) throws SQLException
  77.     {
  78.         this(dataSource, null, user, password);
  79.     }

  80.     public DatabaseDataSourceConnection(DataSource dataSource, String schema)
  81.             throws SQLException
  82.     {
  83.         this(dataSource, schema, null, null);
  84.     }

  85.     public DatabaseDataSourceConnection(DataSource dataSource, String schema,
  86.             String user, String password) throws SQLException
  87.     {
  88.         _dataSource = dataSource;
  89.         _schema = schema;
  90.         _user = user;
  91.         _password = password;
  92.     }

  93.     ////////////////////////////////////////////////////////////////////////////
  94.     // IDatabaseConnection interface

  95.     public Connection getConnection() throws SQLException
  96.     {
  97.         logger.debug("getConnection() - start");

  98.         if (_connection == null)
  99.         {
  100.             try
  101.             {
  102.                 if (_user != null) {
  103.                     _connection = _dataSource.getConnection(_user, _password);
  104.                 } else {
  105.                     _connection = _dataSource.getConnection();
  106.                 }
  107.             }
  108.             catch (SQLException e)
  109.             {
  110.                 logger.error("getConnection(): ", e);
  111.                 throw e;
  112.             }
  113.         }
  114.         return _connection;
  115.     }

  116.     public String getSchema()
  117.     {
  118.         return _schema;
  119.     }

  120.     public void close() throws SQLException
  121.     {
  122.         logger.debug("close() - start");

  123.         if (_connection != null)
  124.         {
  125.             _connection.close();
  126.             _connection = null;
  127.         }
  128.     }
  129. }