1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
84
85
86
87 public void beginTransaction() {
88 if (hasOpenTransaction())
89 throw new JDBCAdaptorException("Cannot nest transactions.", null);
90 transactionDidBegin();
91 }
92
93
94
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
106
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
118
119
120 public EOAdaptorChannel createAdaptorChannel() {
121 JDBCChannel channel = new JDBCChannel(this);
122 _channels.addObject(channel);
123 return channel;
124 }
125
126
127
128
129 public void handleDroppedConnection() {
130
131
132 }
133
134 }
135
136
137
138
139
140
141
142
143
144
145
146