KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minerva > pool > jdbc > PoolDriver


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

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

29 public class PoolDriver implements Driver {
30
31     private final static String JavaDoc URL_START = "jdbc:minerva:";
32     private final static PoolDriver instance;
33
34     static {
35         instance = new PoolDriver();
36         try {
37             DriverManager.registerDriver(PoolDriver.instance());
38         } catch (SQLException e) {
39             System.out.println("Unable to register Minerva DB pool driver!");
40             e.printStackTrace();
41         }
42     }
43
44     /**
45      * Gets the singleton driver instance.
46      */

47     public static PoolDriver instance() {
48         return instance;
49     }
50
51     private PoolDriver() {
52     }
53
54     /**
55      * Tells which URLs this driver can handle.
56      */

57     public boolean acceptsURL(String JavaDoc url) throws java.sql.SQLException JavaDoc {
58         return url.startsWith(URL_START);
59     }
60
61     /**
62      * Retrieves a connection from a connection pool.
63      */

64     public Connection connect(String JavaDoc url, Properties JavaDoc props) throws java.sql.SQLException JavaDoc {
65         if (url.startsWith(URL_START)) {
66             return getJDBCConnection(url.substring(URL_START.length()));
67         }
68         return null; // No SQL Exception here!
69
}
70
71     private Connection getJDBCConnection(String JavaDoc name) {
72         Connection con = null;
73         try {
74             DataSource JavaDoc source = JDBCPoolDataSource.getDataSource(name);
75             if (source != null)
76                 con = source.getConnection();
77         } catch (Exception JavaDoc e) {
78             e.printStackTrace();
79         }
80         return con;
81     }
82
83     /**
84      * Returns the driver version.
85      */

86     public int getMajorVersion() {
87         return 2;
88     }
89
90     /**
91      * Returns the driver version.
92      */

93     public int getMinorVersion() {
94         return 0;
95     }
96
97     /**
98      * Returns no properties. You do not need properties to connect to the
99      * pool, and the properties for the underlying driver are not managed here.
100      */

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

109     public boolean jdbcCompliant() {
110         return false;
111     }
112 }
113
Popular Tags