1 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 41 public class AbstractConnectionFactory implements DBMSConstants 42 { 43 private static final String RCSRevision = "$Revision: 1.2 $"; 44 private static final String RCSName = "$Name: $"; 45 46 private String URL; 50 private short databaseVendor = -1; 51 52 53 public AbstractConnectionFactory(String URL) 57 { 58 this.URL = URL; 59 } 60 61 public String getURL() { return URL;} 65 66 68 public short getDatabaseType() 69 { 70 return databaseVendor; 71 } 72 73 public AbstractConnection getAbstractConnection(String user, String 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 93 short databaseVendor = DBMSInfoMap.getDatabaseVendor(conn); 94 95 96 if (!preserveCommit) 97 conn.setAutoCommit(true); 98 99 100 setTransactionIsolation(conn, databaseVendor); 101 102 return getAbstractConnection(conn, databaseVendor); 103 } 104 105 public String toString() 106 { 107 return URL; 108 } 109 110 private static AbstractConnection getAbstractConnection(Connection conn, short databaseVendor) throws SQLException 114 { 115 Connection tmp = null; 116 117 String url = conn.getMetaData().getURL(); 118 119 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 { 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 } 172 } 173 return false; 174 } 175 return true; 176 } 177 178 private Connection newConnection(String user, String password) 179 throws SQLException 180 { 181 Connection conn = null; 182 if (user == null) 184 conn = DriverManager.getConnection(URL); 185 else 186 conn = DriverManager.getConnection(URL, user, password); 187 188 189 if (databaseVendor == -1) 190 databaseVendor = DBMSInfoMap.getDatabaseVendor(conn); 191 192 193 conn.setAutoCommit(true); 195 196 setTransactionIsolation(conn, databaseVendor); 197 198 return conn; 199 } 200 201 203 private static void setTransactionIsolation(Connection conn, short databaseVendor) throws SQLException 204 { 205 switch (databaseVendor) 206 { 207 case TIMESTEN40 : 208 conn.setTransactionIsolation(4096); 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 |