Coverage Report - net.wotonomy.jdbcadaptor.JDBCContext
 
Classes in this File Line Coverage Branch Coverage Complexity
JDBCContext
0% 
0% 
2.667
 
 1  
 /*
 2  
  Wotonomy: OpenStep design patterns for pure Java applications.
 3  
  Copyright (C) 2001 Michael Powers
 4  
 
 5  
  This library is free software; you can redistribute it and/or
 6  
  modify it under the terms of the GNU Lesser General Public
 7  
  License as published by the Free Software Foundation; either
 8  
  version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
  This library is distributed in the hope that it will be useful,
 11  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  Lesser General Public License for more details.
 14  
 
 15  
  You should have received a copy of the GNU Lesser General Public
 16  
  License along with this library; if not, see http://www.gnu.org
 17  
  */
 18  
 package net.wotonomy.jdbcadaptor;
 19  
 
 20  
 import java.sql.Connection;
 21  
 import java.sql.Driver;
 22  
 import java.sql.DriverManager;
 23  
 import java.sql.SQLException;
 24  
 
 25  
 import net.wotonomy.access.EOAdaptor;
 26  
 import net.wotonomy.access.EOAdaptorChannel;
 27  
 import net.wotonomy.access.EOAdaptorContext;
 28  
 
 29  
 /**
 30  
 * Concrete implementation of EOAdaptorContext for use with JDBC.
 31  
 *
 32  
 * @author ezamudio@nasoft.com
 33  
 * @author $Author: cgruber $
 34  
 * @version $Revision: 903 $
 35  
 */
 36  
 public class JDBCContext extends EOAdaptorContext {
 37  
 
 38  
         protected Connection _jdbcConnection;
 39  
 
 40  
         /**
 41  
          * Creates a new instance.
 42  
          * @param adaptor The adaptor this context belongs to.
 43  
          */
 44  
         public JDBCContext(EOAdaptor adaptor) {
 45  0
                 super(adaptor);
 46  0
         }
 47  
 
 48  
         public void connect() throws JDBCAdaptorException {
 49  
                 try {
 50  0
                         if (_jdbcConnection != null && !_jdbcConnection.isClosed())
 51  0
                                 throw new JDBCAdaptorException("Attempt to connect when already connected.", null);
 52  0
                         Class.forName(((JDBCAdaptor)adaptor()).driverName());
 53  0
                         String url = (String)adaptor().connectionDictionary().objectForKey("URL");
 54  0
                         Driver driver = DriverManager.getDriver(url);
 55  0
                         java.util.Properties props = new java.util.Properties();
 56  0
                         props.setProperty("user", (String)adaptor().connectionDictionary().objectForKey("username"));
 57  0
                         props.setProperty("password", (String)adaptor().connectionDictionary().objectForKey("password"));
 58  0
                         _jdbcConnection = driver.connect(url, props);
 59  0
                         _jdbcConnection.setAutoCommit(false);
 60  0
                 } catch (SQLException ex) {
 61  0
                         throw new JDBCAdaptorException("Cannot connect to database", ex);
 62  0
                 } catch (ClassNotFoundException ex) {
 63  0
                         throw new JDBCAdaptorException("Cannot find JDBC driver " + ex.getMessage(), null);
 64  0
                 }
 65  0
         }
 66  
 
 67  
         public void disconnect() throws JDBCAdaptorException {
 68  0
                 if (_jdbcConnection != null) {
 69  
                         try {
 70  0
                                 _jdbcConnection.close();
 71  0
                                 _jdbcConnection = null;
 72  0
                                 transactionDidRollback();
 73  0
                         } catch (SQLException ex) {
 74  0
                                 throw new JDBCAdaptorException("Trying to close connection.", ex);
 75  0
                         }
 76  
                 }
 77  0
         }
 78  
 
 79  
         public Connection connection() {
 80  0
                 return _jdbcConnection;
 81  
         }
 82  
 
 83  
         /* Begins a transaction. Actually it does nothing because it's not
 84  
          * necessary.
 85  
          * @see net.wotonomy.access.EOAdaptorContext#beginTransaction()
 86  
          */
 87  
         public void beginTransaction() {
 88  0
                 if (hasOpenTransaction())
 89  0
                         throw new JDBCAdaptorException("Cannot nest transactions.", null);
 90  0
                 transactionDidBegin();
 91  0
         }
 92  
 
 93  
         /* Commits a transaction.
 94  
          * @see net.wotonomy.access.EOAdaptorContext#commitTransaction()
 95  
          */
 96  
         public void commitTransaction() {
 97  
                 try {
 98  0
                         _jdbcConnection.commit();
 99  0
                         transactionDidCommit();
 100  0
                 } catch (SQLException ex) {
 101  0
                         throw new JDBCAdaptorException("Cannot commit.", ex);
 102  0
                 }
 103  0
         }
 104  
 
 105  
         /* Rolls back a transacion.
 106  
          * @see net.wotonomy.access.EOAdaptorContext#rollbackTransaction()
 107  
          */
 108  
         public void rollbackTransaction() {
 109  
                 try {
 110  0
                         _jdbcConnection.rollback();
 111  0
                         transactionDidRollback();
 112  0
                 } catch (SQLException ex) {
 113  0
                         throw new JDBCAdaptorException("Cannot commit.", ex);
 114  0
                 }
 115  0
         }
 116  
 
 117  
         /* Creates a JDBCChannel instance.
 118  
          * @see net.wotonomy.access.EOAdaptorContext#createAdaptorChannel()
 119  
          */
 120  
         public EOAdaptorChannel createAdaptorChannel() {
 121  0
                 JDBCChannel channel = new JDBCChannel(this);
 122  0
                 _channels.addObject(channel);
 123  0
                 return channel;
 124  
         }
 125  
 
 126  
         /* I don't know what to do here. Throw something, maybe?
 127  
          * @see net.wotonomy.access.EOAdaptorContext#handleDroppedConnection()
 128  
          */
 129  
         public void handleDroppedConnection() {
 130  
                 // TODO Auto-generated method stub
 131  
 
 132  0
         }
 133  
 
 134  
 }
 135  
 /*
 136  
  * $Log$
 137  
  * Revision 1.2  2006/02/18 22:59:22  cgruber
 138  
  * make it compile with maven dependencies and add a cvsignore.
 139  
  *
 140  
  * Revision 1.1  2006/02/16 13:22:23  cgruber
 141  
  * Check in all sources in eclipse-friendly maven-enabled packages.
 142  
  *
 143  
  * Revision 1.1  2003/08/13 20:13:33  chochos
 144  
  * concrete implementation of EOAdaptorContext for use with JDBC. This is the class that holds the physical connection to the database.
 145  
  *
 146  
  */