1 29 package com.caucho.db.jdbc; 30 31 import com.caucho.db.Database; 32 import com.caucho.db.sql.Query; 33 import com.caucho.db.store.Transaction; 34 import com.caucho.log.Log; 35 import com.caucho.util.L10N; 36 37 import java.sql.CallableStatement ; 38 import java.sql.SQLException ; 39 import java.sql.SQLWarning ; 40 import java.sql.Savepoint ; 41 import java.sql.Statement ; 42 import java.util.ArrayList ; 43 import java.util.Map ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public class ConnectionImpl implements java.sql.Connection { 51 private static final L10N L = new L10N(ConnectionImpl.class); 52 private static final Logger log = Log.open(ConnectionImpl.class); 53 54 private Database _db; 55 private PooledConnectionImpl _pooledConnection; 56 57 private boolean _isClosed; 58 private boolean _isAutoCommit = true; 59 60 private Transaction _xa; 61 62 private StatementImpl _statement; 63 private ArrayList <StatementImpl> _statements; 64 65 public ConnectionImpl(PooledConnectionImpl pooledConnection) 66 { 67 _pooledConnection = pooledConnection; 68 _db = pooledConnection.getDatabase(); 69 70 if (_db == null) 71 throw new NullPointerException (); 72 } 73 74 public ConnectionImpl(Database db) 75 { 76 _db = db; 77 78 if (_db == null) 79 throw new NullPointerException (); 80 } 81 82 Database getDatabase() 83 { 84 return _db; 85 } 86 87 public void clearWarnings() 88 { 89 } 90 91 public void setTransaction(Transaction xa) 92 { 93 _xa = xa; 94 } 95 96 public Transaction getTransaction() 97 { 98 if (_isAutoCommit) { 99 Transaction xa = Transaction.create(this); 100 xa.setAutoCommit(true); 103 return xa; 104 } 105 else if (_xa == null) { 106 _xa = Transaction.create(this); 107 108 if (log.isLoggable(Level.FINER)) 109 log.finer("start transaction " + this + " " + _xa); 110 } 111 112 _xa.setAutoCommit(false); 113 114 return _xa; 115 } 116 117 public void commit() 118 throws SQLException 119 { 120 if (log.isLoggable(Level.FINER)) 121 log.finer("commit " + this + " " + _xa); 122 123 Transaction xa = _xa; 124 _xa = null; 125 126 if (xa != null) 127 xa.commit(); 128 } 129 130 public void rollback() 131 throws SQLException 132 { 133 Transaction xa = _xa; 134 _xa = null; 135 136 if (xa != null) { 137 if (log.isLoggable(Level.FINER)) 138 log.finer("rollback " + this + " " + _xa); 139 140 xa.rollback(); 141 } 142 } 143 144 public java.sql.Statement createStatement() 145 throws SQLException 146 { 147 if (_db == null) 148 throw new SQLException (L.l("Connection is already closed")); 149 150 StatementImpl stmt = new StatementImpl(this); 151 152 if (_statement == null) 153 _statement = stmt; 154 else { 155 if (_statements == null) 156 _statements = new ArrayList <StatementImpl>(); 157 _statements.add(stmt); 158 } 159 160 return stmt; 161 } 162 163 public java.sql.Statement createStatement(int resultSetType, 164 int resultSetConcurrency) 165 throws SQLException 166 { 167 return createStatement(); 168 } 169 170 public boolean getAutoCommit() 171 { 172 return _isAutoCommit; 173 } 174 175 public void setAutoCommit(boolean autoCommit) 176 throws SQLException 177 { 178 if (! _isAutoCommit && autoCommit) { 179 Transaction xa = _xa; 180 _xa = null; 181 182 if (xa != null) 183 xa.commit(); 184 } 185 186 _isAutoCommit = autoCommit; 187 } 188 189 public String getCatalog() 190 { 191 return null; 192 } 193 194 public void setCatalog(String catalog) 195 throws SQLException 196 { 197 } 198 199 public java.sql.DatabaseMetaData getMetaData() 200 throws SQLException 201 { 202 return new DatabaseMetaDataImpl(this); 203 } 204 205 public int getTransactionIsolation() 206 { 207 return TRANSACTION_NONE; 208 } 209 210 public void setTransactionIsolation(int level) 211 { 212 } 213 214 public Map getTypeMap() 215 { 216 return null; 217 } 218 219 public void setTypeMap(Map <String ,Class <?>> map) 220 { 221 } 222 223 public SQLWarning getWarnings() 224 { 225 return null; 226 } 227 228 public boolean isClosed() 229 { 230 return _isClosed; 231 } 232 233 public boolean isReadOnly() 234 { 235 return false; 236 } 237 238 public void setReadOnly(boolean readOnly) 239 { 240 } 241 242 public String nativeSQL(String sql) 243 { 244 return null; 245 } 246 247 public CallableStatement prepareCall(String sql) 248 throws SQLException 249 { 250 return null; 251 } 252 253 public CallableStatement prepareCall(String sql, int resultSetType, 254 int resultSetConcurrency) 255 throws SQLException 256 { 257 return null; 258 } 259 260 public java.sql.PreparedStatement prepareStatement(String sql) 261 throws SQLException 262 { 263 return prepareStatementImpl(sql); 264 } 265 266 public java.sql.PreparedStatement prepareStatement(String sql, 267 int autoGeneratedKeys) 268 throws SQLException 269 { 270 PreparedStatementImpl pstmt = prepareStatementImpl(sql); 271 272 if (autoGeneratedKeys == Statement.RETURN_GENERATED_KEYS) 273 pstmt.setReturnGeneratedKeys(true); 274 275 return pstmt; 276 } 277 278 public java.sql.PreparedStatement prepareStatement(String sql, 279 int []columnIndices) 280 throws SQLException 281 { 282 PreparedStatementImpl pstmt = prepareStatementImpl(sql); 283 284 pstmt.setReturnGeneratedKeys(true); 285 286 return pstmt; 287 } 288 289 public java.sql.PreparedStatement prepareStatement(String sql, 290 String []columnNames) 291 throws SQLException 292 { 293 PreparedStatementImpl pstmt = prepareStatementImpl(sql); 294 295 pstmt.setReturnGeneratedKeys(true); 296 297 return pstmt; 298 } 299 300 public java.sql.PreparedStatement prepareStatement(String sql, 301 int resultSetType, 302 int resultSetConcurrency) 303 throws SQLException 304 { 305 return prepareStatement(sql); 306 } 307 308 public java.sql.PreparedStatement prepareStatement(String sql, 309 int resultSetType, 310 int resultSetConcurrency, 311 int resultSetHoldability) 312 throws SQLException 313 { 314 return prepareStatement(sql); 315 } 316 317 320 private PreparedStatementImpl prepareStatementImpl(String sql) 321 throws SQLException 322 { 323 Query query = _db.parseQuery(sql); 324 325 PreparedStatementImpl stmt = new PreparedStatementImpl(this, query); 326 327 if (_statement == null) 328 _statement = stmt; 329 else { 330 if (_statements == null) 331 _statements = new ArrayList <StatementImpl>(); 332 _statements.add(stmt); 333 } 334 335 return stmt; 336 } 337 338 public void rollback(Savepoint savepoint) 339 throws SQLException 340 { 341 } 342 343 public void releaseSavepoint(Savepoint savepoint) 344 throws SQLException 345 { 346 } 347 348 public Savepoint setSavepoint(String savepoint) 349 throws SQLException 350 { 351 return null; 352 } 353 354 public Savepoint setSavepoint() 355 throws SQLException 356 { 357 return null; 358 } 359 360 public int getHoldability() 361 throws SQLException 362 { 363 return 0; 364 } 365 366 public void setHoldability(int hold) 367 throws SQLException 368 { 369 } 370 371 public java.sql.Statement createStatement(int resultSetType, 372 int resultSetConcurrency, 373 int resultSetHoldability) 374 throws SQLException 375 { 376 return createStatement(); 377 } 378 379 public CallableStatement prepareCall(String sql, int resultSetType, 380 int resultSetConcurrency, 381 int holdability) 382 throws SQLException 383 { 384 return null; 385 } 386 387 void closeStatement(StatementImpl stmt) 388 { 389 if (_statement == stmt) 390 _statement = null; 391 392 if (_statements != null) 393 _statements.remove(stmt); 394 } 395 396 public void close() 397 throws SQLException 398 { 399 synchronized (this) { 400 if (_isClosed) 401 return; 402 403 _isClosed = true; 404 _db = null; 405 } 406 407 StatementImpl stmt = _statement; 408 _statement = null; 409 410 if (stmt != null) 411 _statement = null; 412 413 if (_statements != null) { 414 for (int i = 0; i < _statements.size(); i++) { 415 stmt = _statements.get(i); 416 417 stmt.close(); 418 } 419 } 420 421 if (_pooledConnection != null) 422 _pooledConnection.closeEvent(this); 423 } 424 } 425 | Popular Tags |