KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > xa > wrapper > XADataSourceImpl


1 /*
2  * Licensed under the X license (see http://www.x.org/terms.htm)
3  */

4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper;
5
6 import javax.sql.XAConnection JavaDoc;
7 import javax.sql.XADataSource JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.Driver JavaDoc;
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.log4j.Logger;
16
17 /**
18  * Transactional DataSource wrapper for JDBC 1.0 drivers. This is very
19  * lightweight - it just passes requests through to an underlying driver, and
20  * wraps the results with an XAConnection. The XAConnection and corresponding
21  * XAResource are responsible for closing the connection when appropriate.
22  * Note that the underlying driver may perform pooling, but need not. This
23  * class does not add any pooling capabilities.
24  *
25  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
26  */

27 public class XADataSourceImpl implements XADataSource JavaDoc {
28
29     private String JavaDoc url;
30     private String JavaDoc user;
31     private String JavaDoc password;
32     private String JavaDoc driverName;
33     private Driver JavaDoc driver;
34     private Properties JavaDoc properties;
35     private int loginTimeout;
36     private PrintWriter JavaDoc logWriter;
37     private boolean saveStackTrace;
38     private static Logger log = Logger.getLogger(XADataSourceImpl.class);
39
40     /**
41      * Empty constructure for beans, reflection, etc.
42      */

43     public XADataSourceImpl() {
44     }
45
46     /**
47      * Specifies the URL and properties to connect to the underlying driver.
48      * If the properties are null, they will not be used.
49      */

50     public XADataSourceImpl(String JavaDoc url, Properties JavaDoc properties) {
51         this.url = url;
52         this.properties = properties;
53     }
54
55     /**
56      * Gets the JDBC URL used to open an underlying connection.
57      */

58     public String JavaDoc getURL() {
59         return url;
60     }
61
62     /**
63      * Sets the JDBC URL used to open an underlying connection.
64      */

65     public void setURL(String JavaDoc url) {
66         this.url = url;
67     }
68
69     /**
70      * Gets the JDBC user name used to open an underlying connection.
71      */

72     public String JavaDoc getUser() {
73         return user;
74     }
75
76     /**
77      * Sets the JDBC user name used to open an underlying connection.
78      * This is optional - use it only if your underlying driver requires it.
79      */

80     public void setUser(String JavaDoc user) {
81         this.user = user;
82     }
83
84     /**
85      * Gets the JDBC password used to open an underlying connection.
86      */

87     public String JavaDoc getPassword() {
88         return password;
89     }
90
91     /**
92      * Sets the JDBC password used to open an underlying connection.
93      * This is optional - use it only if your underlying driver requires it.
94      */

95     public void setPassword(String JavaDoc password) {
96         this.password = password;
97     }
98
99     public void setDriver(String JavaDoc driverName) {
100         this.driverName = driverName;
101     }
102
103     /**
104      * Gets the JDBC properties used to open an underlying connection.
105      */

106     public Properties JavaDoc getProperties() {
107         return properties;
108     }
109
110     /**
111      * Sets the JDBC properties used to open an underlying connection.
112      * This is optional - use it only if your underlying driver requires it.
113      */

114     public void setProperties(Properties JavaDoc properties) {
115         this.properties = properties;
116     }
117
118     /**
119      * Have XAClientConnections save a stack trace on creation
120      * This is useful for debugging non-closed connections.
121      * It must be used with ReleaseOnCommit option
122      */

123     public boolean getSaveStackTrace() {
124         return saveStackTrace;
125     }
126
127     public void setSaveStackTrace(boolean save) {
128         saveStackTrace = save;
129     }
130
131     /**
132      * Gets the log writer used to record when XAConnections are opened.
133      */

134     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
135         return logWriter;
136     }
137
138     /**
139      * Sets a log writer used to record when XAConnections are opened.
140      */

141     public void setLogWriter(PrintWriter JavaDoc writer) throws SQLException JavaDoc {
142         if (writer == null) {
143             logWriter = null;
144         }
145
146     }
147
148     /**
149      * This is not used by the current implementation, since the effect would
150      * differ depending on the underlying driver.
151      */

152     public int getLoginTimeout() throws SQLException JavaDoc {
153         return loginTimeout;
154     }
155
156     /**
157      * This is not used by the current implementation, since the effect would
158      * differ depending on the underlying driver.
159      */

160     public void setLoginTimeout(int timeout) throws SQLException JavaDoc {
161         loginTimeout = timeout;
162     }
163
164     private void loadDriver() throws SQLException JavaDoc {
165         if (driver == null) {
166             try {
167                 driver = (Driver JavaDoc) Class.forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance();
168                 DriverManager.registerDriver(driver);
169             } catch (ClassNotFoundException JavaDoc e) {
170                 log.warn("unable to load driver", e);
171             } catch (InstantiationException JavaDoc e) {
172                 log.warn("unable to instantiate driver", e);
173             } catch (IllegalAccessException JavaDoc e) {
174                 log.warn("illegal access exception", e);
175             }
176         }
177     }
178
179     /**
180      * Gets an XAConnection. This first gets a java.sql.Connection from the
181      * underlying driver, and then wraps it in an XAConnection and XAResource.
182      * This uses the URL, user, password, and properties (or as many as you
183      * have specified) to make the connection.
184      */

185     public XAConnection JavaDoc getXAConnection() throws SQLException JavaDoc {
186
187         loadDriver();
188
189         Connection JavaDoc con;
190         if (user != null && user.length() > 0)
191             con = DriverManager.getConnection(url, user, password);
192         else if (properties != null)
193             con = DriverManager.getConnection(url, properties);
194         else
195             con = DriverManager.getConnection(url);
196
197
198         try {
199             con.setAutoCommit(false);
200         } catch (SQLException JavaDoc e) {
201             log.warn("Unable to disable auto-commit on " + con.getClass().getName());
202         }
203
204         XAResourceImpl res = new XAResourceImpl(con);
205         XAConnectionImpl xacon = new XAConnectionImpl(con, res, saveStackTrace);
206         res.setXAConnection(xacon);
207
208
209         log.debug("created new Connection(" + con.getClass().getName() + ") with XAResource " + res.getClass().getName() + " and XAConnection " + xacon.getClass().getName() + ".");
210
211         return xacon;
212     }
213
214     /**
215      * Gets an XAConnection. This first gets a java.sql.Connection from the
216      * underlying driver, and then wraps it in an XAConnection and XAResource.
217      * Note that we never change the default userid and password, but instead
218      * only set the userid and password for this one connection.
219      */

220     public XAConnection JavaDoc getXAConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
221
222         loadDriver();
223         Connection JavaDoc con = DriverManager.getConnection(url, user, password);
224
225         try {
226             con.setAutoCommit(false);
227         } catch (SQLException JavaDoc e) {
228             if (logWriter != null)
229                 logWriter.println("XADataSource unable to disable auto-commit on " + con.getClass().getName());
230         }
231
232         XAResourceImpl res = new XAResourceImpl(con);
233         XAConnectionImpl xacon = new XAConnectionImpl(con, res, saveStackTrace);
234         res.setXAConnection(xacon);
235
236         xacon.setUser(user);
237         xacon.setPassword(password);
238
239         log.debug(" created new Connection (" + con.getClass().getName() + ") with XAResource " + res.getClass().getName() + " and XAConnection with userid and password " + xacon.getClass().getName());
240
241
242         return xacon;
243     }
244 }
245
Popular Tags