1 25 package org.objectweb.jonas.jdbc; 26 27 import java.io.PrintWriter ; 28 import java.util.Map ; 29 30 import java.sql.CallableStatement ; 31 import java.sql.Connection ; 32 import java.sql.DatabaseMetaData ; 33 import java.sql.PreparedStatement ; 34 import java.sql.SQLException ; 35 import java.sql.SQLWarning ; 36 import java.sql.Statement ; 37 38 import javax.resource.ResourceException ; 39 40 import org.objectweb.jonas.resource.MCInfo; 41 import org.objectweb.jonas.resource.SQLManager; 42 43 import org.objectweb.util.monolog.api.BasicLevel; 44 import org.objectweb.util.monolog.api.Logger; 45 46 51 public class ConnectionImpl implements Connection { 52 53 public Logger trace = null; 54 55 ManagedConnectionImpl mc = null; 56 57 Connection connection = null; 58 59 PrintWriter pw = null; 60 61 long key = 0; 62 63 String user = ""; 64 65 private SQLManager conman = null; 66 67 private MCInfo mci = null; 68 69 72 private final boolean isDebugging; 73 74 protected ConnectionImpl(ManagedConnectionImpl _mc, Connection _con, long _key, PrintWriter _pw) { 75 mc = _mc; 76 connection = _con; 77 key = _key; 78 pw = _pw; 79 trace = mc.trace; 80 isDebugging = trace.isLoggable(BasicLevel.DEBUG); 81 try { 82 user = mc.getMetaData().getUserName(); 83 } catch (Exception ex) { 84 } 85 } 86 87 public void setJonasInfo(MCInfo _mci, SQLManager _conman) { 88 mci = _mci; 89 conman = _conman; 90 } 91 92 public boolean isPhysicallyClosed() throws SQLException { 93 if (isDebugging) { 94 trace.log(BasicLevel.DEBUG, ""); 95 } 96 return (connection.isClosed()); 97 } 98 99 101 public void clearWarnings() throws SQLException { 102 if (isDebugging) { 103 trace.log(BasicLevel.DEBUG, ""); 104 } 105 try { 106 checkContext(); 107 } catch (Exception e) { 108 trace.log(BasicLevel.ERROR, "checkContext error"); 109 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 110 } 111 connection.clearWarnings(); 112 } 113 114 public void close() throws SQLException { 115 if (isDebugging) { 116 trace.log(BasicLevel.DEBUG, ""); 117 } 118 try { 119 closeConnectionImpl(); 120 } catch (Exception e) { 121 trace.log(BasicLevel.ERROR, "error"); 122 e.printStackTrace(); 123 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 124 } 125 } 126 127 public void commit() throws SQLException { 128 if (isDebugging) { 129 trace.log(BasicLevel.DEBUG, ""); 130 } 131 try { 132 checkContext(); 133 connection.commit(); 134 } catch (Exception e) { 135 trace.log(BasicLevel.ERROR, "error"); 136 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 137 } 138 } 139 140 public Statement createStatement() throws SQLException { 141 if (isDebugging) { 142 trace.log(BasicLevel.DEBUG, ""); 143 } 144 try { 145 checkContext(); 146 } catch (Exception e) { 147 trace.log(BasicLevel.ERROR, "checkContext error", e, "ConnectionImpl", "createStatement"); 148 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 149 } 150 return connection.createStatement(); 151 } 152 153 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { 154 if (isDebugging) { 155 trace.log(BasicLevel.DEBUG, ""); 156 } 157 try { 158 checkContext(); 159 } catch (Exception e) { 160 trace.log(BasicLevel.ERROR, "ConnectionImpl.createStatement: checkContext error"); 161 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 162 } 163 return connection.createStatement(resultSetType, resultSetConcurrency); 164 } 165 166 public boolean getAutoCommit() throws SQLException { 167 if (isDebugging) { 168 trace.log(BasicLevel.DEBUG, ""); 169 } 170 try { 171 checkContext(); 172 return connection.getAutoCommit(); 173 } catch (Exception e) { 174 trace.log(BasicLevel.ERROR, "ConnectionImpl.getAC error"); 175 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 176 } 177 } 178 179 public String getCatalog() throws SQLException { 180 if (isDebugging) { 181 trace.log(BasicLevel.DEBUG, ""); 182 } 183 try { 184 checkContext(); 185 } catch (Exception e) { 186 trace.log(BasicLevel.ERROR, "ConnectionImpl.getCatalog: checkContext error"); 187 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 188 } 189 return connection.getCatalog(); 190 } 191 192 public int getHoldability() throws SQLException { 194 if (isDebugging) { 195 trace.log(BasicLevel.DEBUG, ""); 196 } 197 try { 198 checkContext(); 199 } catch (Exception e) { 200 trace.log(BasicLevel.ERROR, "ConnectionImpl.getHoldability: checkContext error"); 201 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 202 } 203 return connection.getHoldability(); 204 } 205 206 public DatabaseMetaData getMetaData() throws SQLException { 207 if (isDebugging) { 208 trace.log(BasicLevel.DEBUG, ""); 209 } 210 try { 211 checkContext(); 212 } catch (Exception e) { 213 trace.log(BasicLevel.ERROR, "ConnectionImpl.getMetaData: checkContext error"); 214 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 215 } 216 return connection.getMetaData(); 217 } 218 219 public int getTransactionIsolation() throws SQLException { 220 if (isDebugging) { 221 trace.log(BasicLevel.DEBUG, ""); 222 } 223 try { 224 checkContext(); 225 } catch (Exception e) { 226 trace.log(BasicLevel.ERROR, "ConnectionImpl.getTransactionIsolation: checkContext error"); 227 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 228 } 229 return connection.getTransactionIsolation(); 230 } 231 232 public Map getTypeMap() throws SQLException { 233 if (isDebugging) { 234 trace.log(BasicLevel.DEBUG, ""); 235 } 236 try { 237 checkContext(); 238 } catch (Exception e) { 239 trace.log(BasicLevel.ERROR, "ConnectionImpl.getTypeMap: checkContext error"); 240 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 241 } 242 return connection.getTypeMap(); 243 } 244 245 public SQLWarning getWarnings() throws SQLException { 246 if (isDebugging) { 247 trace.log(BasicLevel.DEBUG, ""); 248 } 249 try { 250 checkContext(); 251 } catch (Exception e) { 252 trace.log(BasicLevel.ERROR, "ConnectionImpl.getWarnings: checkContext error"); 253 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 254 } 255 return connection.getWarnings(); 256 } 257 258 public boolean isClosed() throws SQLException { 259 if (isDebugging) { 260 trace.log(BasicLevel.DEBUG, ""); 261 } 262 return (key == 0); 263 } 264 265 public boolean isReadOnly() throws SQLException { 266 if (isDebugging) { 267 trace.log(BasicLevel.DEBUG, ""); 268 } 269 try { 270 checkContext(); 271 } catch (Exception e) { 272 trace.log(BasicLevel.ERROR, "ConnectionImpl.isReadOnly: checkContext error"); 273 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 274 } 275 return connection.isReadOnly(); 276 } 277 278 public String nativeSQL(String sql) throws SQLException { 279 if (isDebugging) { 280 trace.log(BasicLevel.DEBUG, ""); 281 } 282 try { 283 checkContext(); 284 } catch (Exception e) { 285 trace.log(BasicLevel.ERROR, "ConnectionImpl.nativeSQL: checkContext error"); 286 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 287 } 288 return connection.nativeSQL(sql); 289 } 290 291 public CallableStatement prepareCall(String sql) throws SQLException { 292 if (isDebugging) { 293 trace.log(BasicLevel.DEBUG, ""); 294 } 295 try { 296 checkContext(); 297 } catch (Exception e) { 298 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareCall: checkContext error"); 299 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 300 } 301 return connection.prepareCall(sql); 302 } 303 304 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { 305 306 if (isDebugging) { 307 trace.log(BasicLevel.DEBUG, ""); 308 } 309 try { 310 checkContext(); 311 } catch (Exception e) { 312 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareCall: checkContext error"); 313 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 314 } 315 return connection.prepareCall(sql, resultSetType, resultSetConcurrency); 316 } 317 318 public PreparedStatement prepareStatement(String sql) throws SQLException { 319 if (isDebugging) { 320 trace.log(BasicLevel.DEBUG, ""); 321 } 322 try { 323 checkContext(); 324 } catch (Exception e) { 325 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 326 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 327 } 328 if (conman != null) { 329 return conman.getPStatement(mci, connection, user, sql); 330 } else { 331 return connection.prepareStatement(sql); 332 } 333 } 334 335 public PreparedStatement prepareStatement(String sql, int rsettype, int rsetconcurrency) throws SQLException { 336 if (isDebugging) { 337 trace.log(BasicLevel.DEBUG, ""); 338 } 339 try { 340 checkContext(); 341 } catch (Exception e) { 342 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 343 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 344 } 345 if (conman != null) { 346 return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency); 347 } else { 348 return connection.prepareStatement(sql, rsettype, rsetconcurrency); 349 } 350 } 351 352 public void rollback() throws SQLException { 353 if (isDebugging) { 354 trace.log(BasicLevel.DEBUG, ""); 355 } 356 try { 357 checkContext(); 358 connection.rollback(); 359 } catch (Exception e) { 360 trace.log(BasicLevel.ERROR, e.getMessage()); 361 e.printStackTrace(); 362 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 363 } 364 } 365 366 368 public PreparedStatement prepareStatement(String sql, int rsettype, int rsetconcurrency, int holdability) 369 throws SQLException { 370 if (isDebugging) { 371 trace.log(BasicLevel.DEBUG, ""); 372 } 373 try { 374 checkContext(); 375 } catch (Exception e) { 376 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 377 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 378 } 379 if (conman != null) { 380 return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency, holdability); 381 } else { 382 return connection.prepareStatement(sql, rsettype, rsetconcurrency, holdability); 383 } 384 } 385 386 388 public PreparedStatement prepareStatement(String sql, int[] t) throws SQLException { 389 if (isDebugging) { 390 trace.log(BasicLevel.DEBUG, ""); 391 } 392 try { 393 checkContext(); 394 } catch (Exception e) { 395 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 396 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 397 } 398 if (conman != null) { 399 return conman.getPStatement(mci, connection, user, sql, t); 400 } else { 401 return connection.prepareStatement(sql, t); 402 } 403 } 404 405 public PreparedStatement prepareStatement(String sql, String [] t) throws SQLException { 407 if (isDebugging) { 408 trace.log(BasicLevel.DEBUG, ""); 409 } 410 try { 411 checkContext(); 412 } catch (Exception e) { 413 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 414 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 415 } 416 if (conman != null) { 417 return conman.getPStatement(mci, connection, user, sql, t); 418 } else { 419 return connection.prepareStatement(sql, t); 420 } 421 } 422 423 public PreparedStatement prepareStatement(String sql, int autoGenKeys) throws SQLException { 425 if (isDebugging) { 426 trace.log(BasicLevel.DEBUG, ""); 427 } 428 try { 429 checkContext(); 430 } catch (Exception e) { 431 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 432 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 433 } 434 if (conman != null) { 435 return conman.getPStatement(mci, connection, user, sql, autoGenKeys); 436 } else { 437 return connection.prepareStatement(sql, autoGenKeys); 438 } 439 } 440 441 public CallableStatement prepareCall(String sql, int rsettype, int rsetconcurrency, int holdability) 443 throws SQLException { 444 if (isDebugging) { 445 trace.log(BasicLevel.DEBUG, ""); 446 } 447 try { 448 checkContext(); 449 } catch (Exception e) { 450 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 451 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 452 } 453 return connection.prepareCall(sql, rsettype, rsetconcurrency, holdability); 454 } 455 456 public Statement createStatement(int rsettype, int rsetconcurrency, int holdability) throws SQLException { 458 if (isDebugging) { 459 trace.log(BasicLevel.DEBUG, ""); 460 } 461 try { 462 checkContext(); 463 } catch (Exception e) { 464 trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error"); 465 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 466 } 467 return connection.createStatement(rsettype, rsetconcurrency, holdability); 468 } 469 470 public void releaseSavepoint(java.sql.Savepoint sp) throws SQLException { 472 if (isDebugging) { 473 trace.log(BasicLevel.DEBUG, ""); 474 } 475 try { 476 checkContext(); 477 connection.releaseSavepoint(sp); 478 } catch (Exception e) { 479 trace.log(BasicLevel.ERROR, e.getMessage()); 480 e.printStackTrace(); 481 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 482 } 483 } 484 485 public void rollback(java.sql.Savepoint sp) throws SQLException { 487 if (isDebugging) { 488 trace.log(BasicLevel.DEBUG, ""); 489 } 490 try { 491 checkContext(); 492 connection.rollback(sp); 493 } catch (Exception e) { 494 trace.log(BasicLevel.ERROR, e.getMessage()); 495 e.printStackTrace(); 496 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 497 } 498 } 499 500 public java.sql.Savepoint setSavepoint() throws SQLException { 502 if (isDebugging) { 503 trace.log(BasicLevel.DEBUG, ""); 504 } 505 try { 506 checkContext(); 507 return connection.setSavepoint(); 508 } catch (Exception e) { 509 trace.log(BasicLevel.ERROR, e.getMessage()); 510 e.printStackTrace(); 511 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 512 } 513 } 514 515 public java.sql.Savepoint setSavepoint(String name) throws SQLException { 517 if (isDebugging) { 518 trace.log(BasicLevel.DEBUG, ""); 519 } 520 try { 521 checkContext(); 522 return connection.setSavepoint(name); 523 } catch (Exception e) { 524 trace.log(BasicLevel.ERROR, e.getMessage()); 525 e.printStackTrace(); 526 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 527 } 528 } 529 530 public void setAutoCommit(boolean isauto) throws SQLException { 531 if (isDebugging) { 532 trace.log(BasicLevel.DEBUG, ""); 533 } 534 try { 535 checkContext(); 536 connection.setAutoCommit(isauto); 537 } catch (Exception e) { 538 trace.log(BasicLevel.ERROR, "ConnectionImpl.setAC: error"); 539 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 540 } 541 } 542 543 public void setCatalog(String catalog) throws SQLException { 544 if (isDebugging) { 545 trace.log(BasicLevel.DEBUG, ""); 546 } 547 try { 548 checkContext(); 549 } catch (Exception e) { 550 trace.log(BasicLevel.ERROR, "ConnectionImpl.setCatalog: checkContext error"); 551 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 552 } 553 connection.setCatalog(catalog); 554 } 555 556 public void setHoldability(int holdability) throws SQLException { 558 if (isDebugging) { 559 trace.log(BasicLevel.DEBUG, ""); 560 } 561 try { 562 checkContext(); 563 } catch (Exception e) { 564 trace.log(BasicLevel.ERROR, "ConnectionImpl.setCatalog: checkContext error"); 565 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 566 } 567 connection.setHoldability(holdability); 568 } 569 570 public void setReadOnly(boolean readOnly) throws SQLException { 571 if (isDebugging) { 572 trace.log(BasicLevel.DEBUG, ""); 573 } 574 try { 575 checkContext(); 576 } catch (Exception e) { 577 trace.log(BasicLevel.ERROR, "ConnectionImpl.setReadOnly: checkContext error"); 578 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 579 } 580 connection.setReadOnly(readOnly); 581 } 582 583 public void setTransactionIsolation(int level) throws SQLException { 584 if (isDebugging) { 585 trace.log(BasicLevel.DEBUG, ""); 586 } 587 try { 588 checkContext(); 589 } catch (Exception e) { 590 trace.log(BasicLevel.ERROR, "ConnectionImpl.setTransactionIsolation: checkContext error"); 591 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 592 } 593 connection.setTransactionIsolation(level); 594 } 595 596 public void setTypeMap(Map map) throws SQLException { 597 if (isDebugging) { 598 trace.log(BasicLevel.DEBUG, ""); 599 } 600 try { 601 checkContext(); 602 } catch (Exception e) { 603 trace.log(BasicLevel.ERROR, "ConnectionImpl.setTypeMap: checkContext error"); 604 throw new SQLException ("JOnAS JDBC: " + e.getMessage()); 605 } 606 connection.setTypeMap(map); 607 } 608 609 private void closeConnectionImpl() throws ResourceException { 610 if (key != 0) { 611 mc.close(this); 612 key = 0; 613 } 614 } 615 616 private void checkContext() throws Exception { 617 if (key == 0) { 618 throw new Exception ("Connection is closed"); 619 } 620 if (key != mc.getSignature()) { 621 if (mc.getSignature() == 0) { 622 mc.setSignature(key); 623 } else { 624 throw new Exception ("Connection w/sig(" + key + ") is not currect active Connection (" 625 + mc.getSignature() + ")"); 626 } 627 } 628 } 629 630 public void setSignature(long sig) { 631 key = sig; 632 } 633 } 634 | Popular Tags |