View Javadoc

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  		super(adaptor);
46  	}
47  
48  	public void connect() throws JDBCAdaptorException {
49  		try {
50  			if (_jdbcConnection != null && !_jdbcConnection.isClosed())
51  				throw new JDBCAdaptorException("Attempt to connect when already connected.", null);
52  			Class.forName(((JDBCAdaptor)adaptor()).driverName());
53  			String url = (String)adaptor().connectionDictionary().objectForKey("URL");
54  			Driver driver = DriverManager.getDriver(url);
55  			java.util.Properties props = new java.util.Properties();
56  			props.setProperty("user", (String)adaptor().connectionDictionary().objectForKey("username"));
57  			props.setProperty("password", (String)adaptor().connectionDictionary().objectForKey("password"));
58  			_jdbcConnection = driver.connect(url, props);
59  			_jdbcConnection.setAutoCommit(false);
60  		} catch (SQLException ex) {
61  			throw new JDBCAdaptorException("Cannot connect to database", ex);
62  		} catch (ClassNotFoundException ex) {
63  			throw new JDBCAdaptorException("Cannot find JDBC driver " + ex.getMessage(), null);
64  		}
65  	}
66  
67  	public void disconnect() throws JDBCAdaptorException {
68  		if (_jdbcConnection != null) {
69  			try {
70  				_jdbcConnection.close();
71  				_jdbcConnection = null;
72  				transactionDidRollback();
73  			} catch (SQLException ex) {
74  				throw new JDBCAdaptorException("Trying to close connection.", ex);
75  			}
76  		}
77  	}
78  
79  	public Connection connection() {
80  		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  		if (hasOpenTransaction())
89  			throw new JDBCAdaptorException("Cannot nest transactions.", null);
90  		transactionDidBegin();
91  	}
92  
93  	/* Commits a transaction.
94  	 * @see net.wotonomy.access.EOAdaptorContext#commitTransaction()
95  	 */
96  	public void commitTransaction() {
97  		try {
98  			_jdbcConnection.commit();
99  			transactionDidCommit();
100 		} catch (SQLException ex) {
101 			throw new JDBCAdaptorException("Cannot commit.", ex);
102 		}
103 	}
104 
105 	/* Rolls back a transacion.
106 	 * @see net.wotonomy.access.EOAdaptorContext#rollbackTransaction()
107 	 */
108 	public void rollbackTransaction() {
109 		try {
110 			_jdbcConnection.rollback();
111 			transactionDidRollback();
112 		} catch (SQLException ex) {
113 			throw new JDBCAdaptorException("Cannot commit.", ex);
114 		}
115 	}
116 
117 	/* Creates a JDBCChannel instance.
118 	 * @see net.wotonomy.access.EOAdaptorContext#createAdaptorChannel()
119 	 */
120 	public EOAdaptorChannel createAdaptorChannel() {
121 		JDBCChannel channel = new JDBCChannel(this);
122 		_channels.addObject(channel);
123 		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 	}
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  */