KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > dbms > AbstractConnectionFactory


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program 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 program 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 program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.dbms;
24
25 import java.sql.*;
26
27 import org.xquark.jdbc.typing.DBMSConstants;
28 import org.xquark.jdbc.typing.DBMSInfoMap;
29 import org.xquark.mapper.dbms.microsoft.SQS2000Connection;
30 import org.xquark.mapper.dbms.mysql.MySQL323Connection;
31 import org.xquark.mapper.dbms.oracle.O8iConnection;
32 import org.xquark.mapper.dbms.sybase.ASE119Connection;
33 import org.xquark.mapper.dbms.timesten.TT40Connection;
34
35
36 /**
37  * AbstractConnection factory.
38  *
39  * <p>This object holds database vendor specificities routing.</p>
40  */

41 public class AbstractConnectionFactory implements DBMSConstants
42 {
43     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
44     private static final String JavaDoc RCSName = "$Name: $";
45
46     ///////////////////////////////////////////////////////////////
47
// Data
48
///////////////////////////////////////////////////////////////
49
private String JavaDoc URL;
50     private short databaseVendor = -1;
51
52
53     ///////////////////////////////////////////////////////////////
54
// Contructor
55
///////////////////////////////////////////////////////////////
56
public AbstractConnectionFactory(String JavaDoc URL)
57     {
58         this.URL = URL;
59     }
60     
61     ///////////////////////////////////////////////////////////////
62
// Public Methods
63
///////////////////////////////////////////////////////////////
64
public String JavaDoc getURL() { return URL;}
65     
66     /** Get the constant featuring the database vendor and version.
67      **/

68     public short getDatabaseType()
69     {
70         return databaseVendor;
71     }
72
73     public AbstractConnection getAbstractConnection(String JavaDoc user, String JavaDoc password)
74     throws SQLException
75     {
76         return getAbstractConnection(newConnection(user, password), databaseVendor);
77     }
78     
79     public AbstractConnection getAbstractConnection()
80     throws SQLException
81     {
82         return getAbstractConnection(newConnection(null, null), databaseVendor);
83     }
84     
85     public static AbstractConnection newConnection(Connection conn) throws SQLException
86     {
87         return newConnection(conn, false);
88     }
89     
90     public static AbstractConnection newConnection(Connection conn, boolean preserveCommit) throws SQLException
91     {
92         /* memorize database vendor */
93         short databaseVendor = DBMSInfoMap.getDatabaseVendor(conn);
94         
95         /* Set Auto-commit mode */
96         if (!preserveCommit)
97             conn.setAutoCommit(true);
98         
99         /* Set Transaction isolation */
100         setTransactionIsolation(conn, databaseVendor);
101         
102         return getAbstractConnection(conn, databaseVendor);
103     }
104
105     public String JavaDoc toString()
106     {
107         return URL;
108     }
109
110     ///////////////////////////////////////////////////////////////
111
// Private utilities
112
///////////////////////////////////////////////////////////////
113
private static AbstractConnection getAbstractConnection(Connection conn, short databaseVendor) throws SQLException
114     {
115         Connection tmp = null;
116         
117         String JavaDoc url = conn.getMetaData().getURL();
118         
119         /* Create AbstractConnection */
120         AbstractConnection ret;
121         switch (databaseVendor)
122         {
123             case ORACLE8I : case ORACLE8 : case ORACLE :
124                 ret = new O8iConnection(conn, ORACLE, DBMSInfoMap.getDBMSInfo(ORACLE));
125                 break;
126             case MYSQL : case MYSQL323 :
127                 ret = new MySQL323Connection(conn, MYSQL, DBMSInfoMap.getDBMSInfo(MYSQL));
128                 break;
129             case TIMESTEN40 : case TIMESTEN :
130                 ret = new TT40Connection(conn, TIMESTEN, DBMSInfoMap.getDBMSInfo(TIMESTEN));
131                 break;
132             case SYBASE_ASE_11_9 : case SYBASE :
133                 ret = new ASE119Connection(conn, SYBASE, DBMSInfoMap.getDBMSInfo(SYBASE));
134                 break;
135             case SQL_SERVER_2000 : case SQL_SERVER :
136                 ret = new SQS2000Connection(conn, SQL_SERVER, DBMSInfoMap.getDBMSInfo(SQL_SERVER));
137                 break;
138             case JDBC1 :
139                 ret = new JDBC1Connection(conn, databaseVendor);
140                 break;
141             case JDBC2 :
142                 ret = new JDBC2Connection(conn, databaseVendor);
143                 break;
144             default :
145                 throw new SQLException("Your RDBMS is not supported in the current version of the repository (basing on your JDBC driver informations).");
146         }
147         ret.onInitConnection();
148         return ret;
149     }
150
151     private boolean isConnectionOK(Connection conn)
152     {
153         Statement testStmt = null;
154         try {
155             if (!conn.isClosed())
156             { // Try to createStatement to see if it's really alive
157
testStmt = conn.createStatement();
158                 testStmt.close();
159             }
160             else
161                 return false;
162         }
163         catch (SQLException e) {
164             if (testStmt != null)
165             {
166                 try {
167                     testStmt.close();
168                 }
169                 catch (SQLException se) {
170                     // no op
171
}
172             }
173             return false;
174         }
175         return true;
176     }
177
178     private Connection newConnection(String JavaDoc user, String JavaDoc password)
179     throws SQLException
180     {
181         Connection conn = null;
182         //Debug.print(this, "Opening JDBC connection");
183
if (user == null)
184             conn = DriverManager.getConnection(URL);
185         else
186             conn = DriverManager.getConnection(URL, user, password);
187
188         /* memorize database vendor */
189         if (databaseVendor == -1)
190             databaseVendor = DBMSInfoMap.getDatabaseVendor(conn);
191
192         /* Set Auto-commit mode */
193         conn.setAutoCommit(true); // Normally useless : default
194

195         /* Set Transaction isolation */
196         setTransactionIsolation(conn, databaseVendor);
197
198         return conn;
199     }
200
201     /** Set the connection transaction isolation level according to vendor
202      */

203     private static void setTransactionIsolation(Connection conn, short databaseVendor) throws SQLException
204     {
205         switch (databaseVendor)
206         {
207             case TIMESTEN40 :
208                 conn.setTransactionIsolation(4096); // com.timesten.sql.TimesTenConnection.TRANSACTION_CURSOR_STABILITY(TRANSACTION_READ_COMMITTED has important limitations with JDBC)
209
break;
210             default :
211                 if (conn.getMetaData().supportsTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED))
212                     conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
213                 else
214                     throw new SQLException("Your RDBMS does not support the needed transaction isolation level (TRANSACTION_READ_COMMITTED).");
215         }
216     }
217 }
218
Popular Tags