KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > xa > XAPoolDriver


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

4 package org.ofbiz.minerva.pool.jdbc.xa;
5
6 import java.sql.Connection JavaDoc;
7 import java.sql.Driver JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.DriverPropertyInfo JavaDoc;
10 import java.sql.SQLException JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import javax.sql.DataSource JavaDoc;
14
15 import org.apache.log4j.Logger;
16
17 /**
18  * JDBC Driver to access pooled JDBC connections. Supports JDBC 2.0
19  * transactional XAConnections. You will get a java.sql.Connection back
20  * in any case (the transactional-ness is handled under the covers). You
21  * must create the pools ahead of time by creating and initializing the
22  * appropriate DataSource.
23  * <P>You should use a URL of the form
24  * <B>jdbc:minerva:xa:<I>PoolName</I></B> to get a connection from the
25  * pool.</P>
26  * <P>This driver will be loaded automatically whenever an XA Pool is
27  * created, but you can also use the standard <CODE>Class.forName</CODE>
28  * call to load it.</P>
29  * @see org.ofbiz.minerva.pool.jdbc.xa.XAPoolDataSource
30  *
31  * @author Aaron Mulder (ammulder@alumni.princeton.edu)
32  */

33
34 public class XAPoolDriver implements Driver JavaDoc {
35
36     private final static Logger log = Logger.getLogger(XAPoolDriver.class);
37     private final static String JavaDoc URL_START = "jdbc:minerva:xa:";
38     private final static XAPoolDriver instance;
39
40     static {
41         instance = new XAPoolDriver();
42         try {
43             DriverManager.registerDriver(XAPoolDriver.instance());
44         } catch (SQLException JavaDoc e) {
45             log.error("Unable to register Minerva XA DB pool driver!", e);
46         }
47     }
48
49     /**
50      * Gets the singleton driver instance.
51      */

52     public static XAPoolDriver instance() {
53         return instance;
54     }
55
56     private XAPoolDriver() {
57     }
58
59     /**
60      * Tells which URLs this driver can handle.
61      */

62     public boolean acceptsURL(String JavaDoc url) throws java.sql.SQLException JavaDoc {
63         return url.startsWith(URL_START);
64     }
65
66     /**
67      * Retrieves a connection from a connection pool.
68      */

69     public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc props) throws java.sql.SQLException JavaDoc {
70         if (url.startsWith(URL_START)) {
71             return getXAConnection(url.substring(URL_START.length()));
72         }
73         return null; // No SQL Exception here!
74
}
75
76     private Connection JavaDoc getXAConnection(String JavaDoc name) {
77         Connection JavaDoc con = null;
78         try {
79             DataSource JavaDoc source = XAPoolDataSource.getDataSource(name);
80             if (source != null)
81                 con = source.getConnection();
82         } catch (Exception JavaDoc e) {
83             log.error("Can't get DataSource from XAPool", e);
84         }
85         return con;
86     }
87
88     /**
89      * Returns the driver version.
90      */

91     public int getMajorVersion() {
92         return 2;
93     }
94
95     /**
96      * Returns the driver version.
97      */

98     public int getMinorVersion() {
99         return 0;
100     }
101
102     /**
103      * Returns no properties. You do not need properties to connect to the
104      * pool, and the properties for the underlying driver are not managed here.
105      */

106     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
107         return new DriverPropertyInfo JavaDoc[0];
108     }
109
110     /**
111      * Returns <B>false</B> since it is not known which underlying driver will
112      * be used and what its capabilities are.
113      */

114     public boolean jdbcCompliant() {
115         return false;
116     }
117 }
118
119 /*
120 vim:tabstop=3:et:shiftwidth=3
121 */

122
Popular Tags