1 4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.PrintStream ; 8 import java.sql.CallableStatement ; 9 import java.sql.Connection ; 10 import java.sql.DatabaseMetaData ; 11 import java.sql.PreparedStatement ; 12 import java.sql.SQLException ; 13 import java.sql.SQLWarning ; 14 import java.sql.Savepoint ; 15 import java.sql.Statement ; 16 import java.util.Collection ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Vector ; 21 22 import org.ofbiz.minerva.pool.PoolEvent; 23 import org.ofbiz.minerva.pool.cache.LeastRecentlyUsedCache; 24 import org.ofbiz.minerva.pool.cache.ObjectCache; 25 import org.ofbiz.minerva.pool.jdbc.ConnectionInPool; 26 import org.ofbiz.minerva.pool.jdbc.ConnectionWrapper; 27 import org.ofbiz.minerva.pool.jdbc.PreparedStatementFactory; 28 import org.ofbiz.minerva.pool.jdbc.PreparedStatementInPool; 29 import org.ofbiz.minerva.pool.jdbc.StatementInPool; 30 31 import org.apache.log4j.Logger; 32 33 43 public class XAClientConnection implements ConnectionWrapper { 44 45 private final static String CLOSED = "Connection has been closed!"; 46 47 private Connection con; 48 private HashSet statements; 49 private Vector listeners; 50 private XAConnectionImpl xaCon; 51 private int preparedStatementCacheSize = 0; 52 private ObjectCache preparedStatementCache; 53 private String stackTrace = null; 54 private static Logger log = Logger.getLogger(XADataSourceImpl.class); 55 56 61 public XAClientConnection(XAConnectionImpl xaCon, Connection con, boolean saveStackTrace) { 62 this.con = con; 63 this.xaCon = xaCon; 64 preparedStatementCache = (ObjectCache) ConnectionInPool.psCaches.get(con); 65 if (preparedStatementCache == null) { 66 PreparedStatementFactory factory = new PreparedStatementFactory(con); 67 preparedStatementCache = new LeastRecentlyUsedCache(factory, preparedStatementCacheSize); 68 ConnectionInPool.psCaches.put(con, preparedStatementCache); 69 } 70 statements = new HashSet (); 71 listeners = new Vector (); 72 if (saveStackTrace) { 73 try { 74 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 75 PrintStream stream = new PrintStream (baos); 76 new Throwable ().printStackTrace(stream); 77 baos.close(); 78 stackTrace = baos.toString(); 79 } catch (Exception ex) { 80 } 81 } 82 } 83 84 89 public void setPSCacheSize(int maxSize) { 90 preparedStatementCacheSize = maxSize; 91 if (preparedStatementCache != null) { 92 preparedStatementCache.setSize(maxSize); 93 } 94 } 95 96 100 public int getPSCacheSize() { 101 return preparedStatementCacheSize; 102 } 103 104 110 public Connection getUnderlyingConnection() { 111 return con; 112 } 113 114 118 public void shutdown() { 119 con = null; 120 statements = null; 121 listeners = null; 122 xaCon = null; 123 } 124 125 129 public void setLastUsed() { 130 xaCon.firePoolEvent(new PoolEvent(xaCon, PoolEvent.OBJECT_USED)); 131 } 132 133 136 public void setError(SQLException e) { 137 xaCon.setConnectionError(e); 138 } 139 140 145 public void statementClosed(Statement st) { 146 statements.remove(st); 147 if ((con != null) && (st instanceof PreparedStatementInPool) && preparedStatementCacheSize != 0) { 148 149 PreparedStatementInPool ps = (PreparedStatementInPool) st; 151 PreparedStatement ups = ps.getUnderlyingPreparedStatement(); 152 preparedStatementCache.returnObject(ps.getSql(), ups); 153 166 } 167 } 168 169 public Statement createStatement() throws SQLException { 171 if (con == null) throw new SQLException (CLOSED); 172 try { 173 StatementInPool st = new StatementInPool(con.createStatement(), this); 174 statements.add(st); 175 return st; 176 } catch (SQLException e) { 177 setError(e); 178 throw e; 179 } 180 } 181 182 public PreparedStatement prepareStatement(String sql) throws SQLException { 183 if (con == null) throw new SQLException (CLOSED); 184 try { 185 PreparedStatement ps; 186 if (preparedStatementCacheSize == 0) { 187 ps = con.prepareStatement(sql); 189 } else { 190 ps = (PreparedStatement ) preparedStatementCache.useObject(sql); 191 } 192 if (ps == null) 193 throw new SQLException ("Unable to create PreparedStatement!"); 194 PreparedStatementInPool wrapper = new PreparedStatementInPool(ps, this, sql); 195 statements.add(wrapper); 196 return wrapper; 197 } catch (SQLException e) { 198 setError(e); 199 throw e; 200 } 201 } 202 203 public CallableStatement prepareCall(String sql) throws SQLException { 204 if (con == null) throw new SQLException (CLOSED); 205 try { 206 return con.prepareCall(sql); 207 } catch (SQLException e) { 208 setError(e); 209 throw e; 210 } 211 } 212 213 public String nativeSQL(String sql) throws SQLException { 214 if (con == null) throw new SQLException (CLOSED); 215 try { 216 return con.nativeSQL(sql); 217 } catch (SQLException e) { 218 setError(e); 219 throw e; 220 } 221 } 222 223 public void setAutoCommit(boolean autoCommit) throws SQLException { 224 if (con == null) throw new SQLException (CLOSED); 225 if (((XAResourceImpl) xaCon.getXAResource()).isTransaction() && autoCommit) 226 throw new SQLException ("Cannot set AutoCommit for a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)"); 227 228 try { 229 con.setAutoCommit(autoCommit); 230 } catch (SQLException e) { 231 setError(e); 232 throw e; 233 } 234 235 } 236 237 public boolean getAutoCommit() throws SQLException { 238 if (con == null) throw new SQLException (CLOSED); 239 try { 240 return con.getAutoCommit(); 241 } catch (SQLException e) { 242 setError(e); 243 throw e; 244 } 245 } 246 247 public void commit() throws SQLException { 248 if (con == null) throw new SQLException (CLOSED); 249 if (((XAResourceImpl) xaCon.getXAResource()).isTransaction()) 250 throw new SQLException ("Cannot commit a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)"); 251 try { 252 con.commit(); 253 } catch (SQLException e) { 254 setError(e); 255 throw e; 256 } 257 } 258 259 public void rollback() throws SQLException { 260 if (con == null) throw new SQLException (CLOSED); 261 if (((XAResourceImpl) xaCon.getXAResource()).isTransaction()) 262 throw new SQLException ("Cannot rollback a transactional connection: See JDBC 2.0 Optional Package Specification section 7.1 (p25)"); 263 } 264 265 public void forcedClose() throws SQLException { 266 if (stackTrace != null) 267 System.err.println("A forced close because a non-closed connection:\n" + stackTrace); 268 if (con == null) throw new SQLException (CLOSED); 269 Collection copy = (Collection ) statements.clone(); 270 Iterator it = copy.iterator(); 271 while (it.hasNext()) 272 try { 273 ((Statement ) it.next()).close(); 274 } catch (SQLException e) { 275 } 276 shutdown(); 277 } 278 279 public void close() throws SQLException { 280 if (con == null) throw new SQLException (CLOSED); 281 Collection copy = (Collection ) statements.clone(); 282 Iterator it = copy.iterator(); 283 while (it.hasNext()) 284 try { 285 ((Statement ) it.next()).close(); 286 } catch (SQLException e) { 287 log.warn("SQLException : ", e); 288 } 289 290 xaCon.clientConnectionClosed(this); 291 shutdown(); 292 } 293 294 public boolean isClosed() throws SQLException { 295 if (con == null) return true; 296 try { 297 return con.isClosed(); 298 } catch (SQLException e) { 299 setError(e); 300 throw e; 301 } 302 } 303 304 public DatabaseMetaData getMetaData() throws SQLException { 305 if (con == null) throw new SQLException (CLOSED); 306 try { 307 return con.getMetaData(); 308 } catch (SQLException e) { 309 setError(e); 310 throw e; 311 } 312 } 313 314 public void setReadOnly(boolean readOnly) throws SQLException { 315 if (con == null) throw new SQLException (CLOSED); 316 try { 317 con.setReadOnly(readOnly); 318 } catch (SQLException e) { 319 setError(e); 320 throw e; 321 } 322 } 323 324 public boolean isReadOnly() throws SQLException { 325 if (con == null) throw new SQLException (CLOSED); 326 try { 327 return con.isReadOnly(); 328 } catch (SQLException e) { 329 setError(e); 330 throw e; 331 } 332 } 333 334 public void setCatalog(String catalog) throws SQLException { 335 if (con == null) throw new SQLException (CLOSED); 336 try { 337 con.setCatalog(catalog); 338 } catch (SQLException e) { 339 setError(e); 340 throw e; 341 } 342 } 343 344 public String getCatalog() throws SQLException { 345 if (con == null) throw new SQLException (CLOSED); 346 try { 347 return con.getCatalog(); 348 } catch (SQLException e) { 349 setError(e); 350 throw e; 351 } 352 } 353 354 public void setTransactionIsolation(int level) throws SQLException { 355 if (con == null) throw new SQLException (CLOSED); 356 try { 357 con.setTransactionIsolation(level); 358 } catch (SQLException e) { 359 setError(e); 360 throw e; 361 } 362 } 363 364 public int getTransactionIsolation() throws SQLException { 365 if (con == null) throw new SQLException (CLOSED); 366 try { 367 return con.getTransactionIsolation(); 368 } catch (SQLException e) { 369 setError(e); 370 throw e; 371 } 372 } 373 374 public SQLWarning getWarnings() throws SQLException { 375 if (con == null) throw new SQLException (CLOSED); 376 try { 377 return con.getWarnings(); 378 } catch (SQLException e) { 379 setError(e); 380 throw e; 381 } 382 } 383 384 public void clearWarnings() throws SQLException { 385 if (con == null) throw new SQLException (CLOSED); 386 try { 387 con.clearWarnings(); 388 } catch (SQLException e) { 389 setError(e); 390 throw e; 391 } 392 } 393 394 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 395 if (con == null) throw new SQLException (CLOSED); 396 try { 397 StatementInPool st = new StatementInPool(con.createStatement(resultSetType, resultSetConcurrency), this); 398 statements.add(st); 399 return st; 400 } catch (SQLException e) { 401 setError(e); 402 throw e; 403 } 404 } 405 406 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 407 if (con == null) throw new SQLException (CLOSED); 408 try { 409 return con.prepareStatement(sql, resultSetType, resultSetConcurrency); 410 } catch (SQLException e) { 411 setError(e); 412 throw e; 413 } 414 } 415 416 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 417 if (con == null) throw new SQLException (CLOSED); 418 try { 419 return con.prepareCall(sql, resultSetType, resultSetConcurrency); 420 } catch (SQLException e) { 421 setError(e); 422 throw e; 423 } 424 } 425 426 public Map getTypeMap() throws SQLException { 427 if (con == null) throw new SQLException (CLOSED); 428 try { 429 return con.getTypeMap(); 430 } catch (SQLException e) { 431 setError(e); 432 throw e; 433 } 434 } 435 436 public void setTypeMap(Map map) throws SQLException { 437 if (con == null) throw new SQLException (CLOSED); 438 try { 439 con.setTypeMap(map); 440 } catch (SQLException e) { 441 setError(e); 442 throw e; 443 } 444 } 445 446 448 451 public void setHoldability(int arg0) throws SQLException { 452 throw new SQLException ("Method not implemented!"); 453 454 } 455 456 459 public int getHoldability() throws SQLException { 460 throw new SQLException ("Method not implemented!"); 461 } 462 463 466 public Savepoint setSavepoint() throws SQLException { 467 throw new SQLException ("Method not implemented!"); 468 } 469 470 473 public Savepoint setSavepoint(String arg0) throws SQLException { 474 throw new SQLException ("Method not implemented!"); 475 } 476 477 480 public void rollback(Savepoint arg0) throws SQLException { 481 throw new SQLException ("Method not implemented!"); 482 483 } 484 485 488 public void releaseSavepoint(Savepoint arg0) throws SQLException { 489 throw new SQLException ("Method not implemented!"); 490 491 } 492 493 496 public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException { 497 throw new SQLException ("Method not implemented!"); 498 } 499 500 503 public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException { 504 throw new SQLException ("Method not implemented!"); 505 } 506 507 510 public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException { 511 throw new SQLException ("Method not implemented!"); 512 } 513 514 517 public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException { 518 throw new SQLException ("Method not implemented!"); 519 } 520 521 524 public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException { 525 throw new SQLException ("Method not implemented!"); 526 } 527 528 531 public PreparedStatement prepareStatement(String arg0, String [] arg1) throws SQLException { 532 throw new SQLException ("Method not implemented!"); 533 } 534 } 535 | Popular Tags |