00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package org.objectweb.jonas.jdbc;
00027
00028 import java.io.PrintWriter;
00029 import java.util.Map;
00030
00031 import java.sql.CallableStatement;
00032 import java.sql.Connection;
00033 import java.sql.DatabaseMetaData;
00034 import java.sql.PreparedStatement;
00035 import java.sql.SQLException;
00036 import java.sql.SQLWarning ;
00037 import java.sql.Statement;
00038
00039 import javax.resource.ResourceException;
00040
00041 import org.objectweb.jonas.resource.MCInfo;
00042 import org.objectweb.jonas.resource.SQLManager;
00043
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045 import org.objectweb.util.monolog.api.Logger;
00046
00047
00052 public class ConnectionImpl
00053 implements Connection {
00054
00055 public Logger trace = null;
00056
00057 ManagedConnectionImpl mc = null;
00058 Connection connection = null;
00059 PrintWriter pw = null;
00060 long key = 0;
00061 String user = "";
00062
00063 private SQLManager conman = null;
00064 private MCInfo mci = null;
00065
00066 protected ConnectionImpl(ManagedConnectionImpl _mc, Connection _con,
00067 long _key, PrintWriter _pw) {
00068 mc = _mc;
00069 connection = _con;
00070 key = _key;
00071 pw = _pw;
00072 trace = mc.trace;
00073 try {
00074 user = mc.getMetaData().getUserName();
00075 } catch (Exception ex) {
00076 }
00077 }
00078
00079 public void setJonasInfo(MCInfo _mci, SQLManager _conman) {
00080 mci = _mci;
00081 conman = _conman;
00082 }
00083
00084 public boolean isPhysicallyClosed() throws SQLException {
00085 if (trace.isLoggable(BasicLevel.DEBUG)) {
00086 trace.log(BasicLevel.DEBUG,"");
00087 }
00088 return (connection.isClosed());
00089 }
00090
00091
00092 public void clearWarnings() throws SQLException {
00093 if (trace.isLoggable(BasicLevel.DEBUG)) {
00094 trace.log(BasicLevel.DEBUG,"");
00095 }
00096 try {
00097 checkContext();
00098 } catch (Exception e) {
00099 trace.log(BasicLevel.ERROR,"checkContext error");
00100 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00101 }
00102 connection.clearWarnings();
00103 }
00104
00105 public void close() throws SQLException {
00106 if (trace.isLoggable(BasicLevel.DEBUG)) {
00107 trace.log(BasicLevel.DEBUG,"");
00108 }
00109 try {
00110 closeConnectionImpl();
00111 } catch (Exception e) {
00112 trace.log(BasicLevel.ERROR,"error");
00113 e.printStackTrace();
00114 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00115 }
00116 }
00117
00118 public void commit() throws SQLException {
00119 if (trace.isLoggable(BasicLevel.DEBUG)) {
00120 trace.log(BasicLevel.DEBUG,"");
00121 }
00122 try {
00123 checkContext();
00124 connection.commit();
00125 } catch (Exception e) {
00126 trace.log(BasicLevel.ERROR,"error");
00127 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00128 }
00129 }
00130
00131 public Statement createStatement()
00132 throws SQLException {
00133 if (trace.isLoggable(BasicLevel.DEBUG)) {
00134 trace.log(BasicLevel.DEBUG,"");
00135 }
00136 try {
00137 checkContext();
00138 } catch (Exception e) {
00139 trace.log(BasicLevel.ERROR,"checkContext error", e,"ConnectionImpl"
00140 ,"createStatement");
00141 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00142 }
00143 return connection.createStatement();
00144 }
00145
00146 public Statement createStatement(int resultSetType,
00147 int resultSetConcurrency)
00148 throws SQLException {
00149 if (trace.isLoggable(BasicLevel.DEBUG)) {
00150 trace.log(BasicLevel.DEBUG,"");
00151 }
00152 try {
00153 checkContext();
00154 } catch (Exception e) {
00155 trace.log(BasicLevel.ERROR,
00156 "ConnectionImpl.createStatement: checkContext error");
00157 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00158 }
00159 return connection
00160 .createStatement(resultSetType, resultSetConcurrency);
00161 }
00162
00163 public boolean getAutoCommit() throws SQLException {
00164 if (trace.isLoggable(BasicLevel.DEBUG)) {
00165 trace.log(BasicLevel.DEBUG,"");
00166 }
00167 try {
00168 checkContext();
00169 return connection.getAutoCommit();
00170 } catch (Exception e) {
00171 trace.log(BasicLevel.ERROR,"ConnectionImpl.getAC error");
00172 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00173 }
00174 }
00175
00176 public String getCatalog() throws SQLException {
00177 if (trace.isLoggable(BasicLevel.DEBUG)) {
00178 trace.log(BasicLevel.DEBUG,"");
00179 }
00180 try {
00181 checkContext();
00182 } catch (Exception e) {
00183 trace.log(BasicLevel.ERROR,
00184 "ConnectionImpl.getCatalog: checkContext error");
00185 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00186 }
00187 return connection.getCatalog();
00188 }
00189
00190 public int getHoldability()
00191 throws SQLException {
00192 trace.log(BasicLevel.DEBUG,"");
00193 try {
00194 checkContext();
00195 } catch (Exception e) {
00196 trace.log(BasicLevel.ERROR,
00197 "ConnectionImpl.getHoldability: checkContext error");
00198 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00199 }
00200 return connection.getHoldability();
00201 }
00202
00203 public DatabaseMetaData getMetaData()
00204 throws SQLException {
00205 if (trace.isLoggable(BasicLevel.DEBUG)) {
00206 trace.log(BasicLevel.DEBUG,"");
00207 }
00208 try {
00209 checkContext();
00210 } catch (Exception e) {
00211 trace.log(BasicLevel.ERROR,
00212 "ConnectionImpl.getMetaData: checkContext error");
00213 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00214 }
00215 return connection.getMetaData();
00216 }
00217
00218 public int getTransactionIsolation()
00219 throws SQLException {
00220 if (trace.isLoggable(BasicLevel.DEBUG)) {
00221 trace.log(BasicLevel.DEBUG,"");
00222 }
00223 try {
00224 checkContext();
00225 } catch (Exception e) {
00226 trace.log(BasicLevel.ERROR,
00227 "ConnectionImpl.getTransactionIsolation: checkContext error");
00228 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00229 }
00230 return connection.getTransactionIsolation();
00231 }
00232
00233 public Map getTypeMap() throws SQLException {
00234 if (trace.isLoggable(BasicLevel.DEBUG)) {
00235 trace.log(BasicLevel.DEBUG,"");
00236 }
00237 try {
00238 checkContext();
00239 } catch (Exception e) {
00240 trace.log(BasicLevel.ERROR,
00241 "ConnectionImpl.getTypeMap: checkContext error");
00242 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00243 }
00244 return connection.getTypeMap();
00245 }
00246
00247 public SQLWarning getWarnings() throws SQLException {
00248 if (trace.isLoggable(BasicLevel.DEBUG)) {
00249 trace.log(BasicLevel.DEBUG,"");
00250 }
00251 try {
00252 checkContext();
00253 } catch (Exception e) {
00254 trace.log(BasicLevel.ERROR,
00255 "ConnectionImpl.getWarnings: checkContext error");
00256 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00257 }
00258 return connection.getWarnings();
00259 }
00260
00261 public boolean isClosed() throws SQLException {
00262 if (trace.isLoggable(BasicLevel.DEBUG)) {
00263 trace.log(BasicLevel.DEBUG,"");
00264 }
00265 return (key == 0);
00266 }
00267
00268 public boolean isReadOnly() throws SQLException {
00269 if (trace.isLoggable(BasicLevel.DEBUG)) {
00270 trace.log(BasicLevel.DEBUG,"");
00271 }
00272 try {
00273 checkContext();
00274 } catch (Exception e) {
00275 trace.log(BasicLevel.ERROR,
00276 "ConnectionImpl.isReadOnly: checkContext error");
00277 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00278 }
00279 return connection.isReadOnly();
00280 }
00281
00282 public String nativeSQL(String sql) throws SQLException {
00283 if (trace.isLoggable(BasicLevel.DEBUG)) {
00284 trace.log(BasicLevel.DEBUG,"");
00285 }
00286 try {
00287 checkContext();
00288 } catch (Exception e) {
00289 trace.log(BasicLevel.ERROR,
00290 "ConnectionImpl.nativeSQL: checkContext error");
00291 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00292 }
00293 return connection.nativeSQL(sql);
00294 }
00295
00296 public CallableStatement prepareCall(String sql) throws SQLException {
00297 if (trace.isLoggable(BasicLevel.DEBUG)) {
00298 trace.log(BasicLevel.DEBUG,"");
00299 }
00300 try {
00301 checkContext();
00302 } catch (Exception e) {
00303 trace.log(BasicLevel.ERROR,
00304 "ConnectionImpl.prepareCall: checkContext error");
00305 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00306 }
00307 return connection.prepareCall(sql);
00308 }
00309
00310 public CallableStatement prepareCall(String sql,
00311 int resultSetType, int resultSetConcurrency) throws SQLException {
00312
00313 if (trace.isLoggable(BasicLevel.DEBUG)) {
00314 trace.log(BasicLevel.DEBUG,"");
00315 }
00316 try {
00317 checkContext();
00318 } catch (Exception e) {
00319 trace.log(BasicLevel.ERROR,
00320 "ConnectionImpl.prepareCall: checkContext error");
00321 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00322 }
00323 return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
00324 }
00325
00326 public PreparedStatement prepareStatement(String sql)
00327 throws SQLException {
00328 if (trace.isLoggable(BasicLevel.DEBUG)) {
00329 trace.log(BasicLevel.DEBUG,"");
00330 }
00331 try {
00332 checkContext();
00333 } catch (Exception e) {
00334 trace.log(BasicLevel.ERROR,
00335 "ConnectionImpl.prepareStatement: checkContext error");
00336 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00337 }
00338 if (conman != null) {
00339 return conman.getPStatement(mci, connection, user, sql);
00340 } else {
00341 return connection.prepareStatement(sql);
00342 }
00343 }
00344
00345 public PreparedStatement prepareStatement(String sql,
00346 int rsettype,
00347 int rsetconcurrency)
00348 throws SQLException {
00349 if (trace.isLoggable(BasicLevel.DEBUG)) {
00350 trace.log(BasicLevel.DEBUG,"");
00351 }
00352 try {
00353 checkContext();
00354 } catch (Exception e) {
00355 trace.log(BasicLevel.ERROR,
00356 "ConnectionImpl.prepareStatement: checkContext error");
00357 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00358 }
00359 if (conman != null) {
00360 return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency);
00361 } else {
00362 return connection.prepareStatement(sql, rsettype, rsetconcurrency);
00363 }
00364 }
00365
00366 public void rollback() throws SQLException {
00367 if (trace.isLoggable(BasicLevel.DEBUG)) {
00368 trace.log(BasicLevel.DEBUG,"");
00369 }
00370 try {
00371 checkContext();
00372 connection.rollback();
00373 } catch (Exception e) {
00374 trace.log(BasicLevel.ERROR,e.getMessage());
00375 e.printStackTrace();
00376 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00377 }
00378 }
00379
00380
00381
00382 public PreparedStatement prepareStatement(String sql,
00383 int rsettype,
00384 int rsetconcurrency,
00385 int holdability)
00386 throws SQLException {
00387 if (trace.isLoggable(BasicLevel.DEBUG)) {
00388 trace.log(BasicLevel.DEBUG,"");
00389 }
00390 try {
00391 checkContext();
00392 } catch (Exception e) {
00393 trace.log(BasicLevel.ERROR,
00394 "ConnectionImpl.prepareStatement: checkContext error");
00395 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00396 }
00397 if (conman != null) {
00398 return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency, holdability);
00399 } else {
00400 return connection.prepareStatement(sql, rsettype, rsetconcurrency, holdability);
00401 }
00402 }
00403
00404
00405
00406 public PreparedStatement prepareStatement(String sql,
00407 int[] t)
00408 throws SQLException {
00409 if (trace.isLoggable(BasicLevel.DEBUG)) {
00410 trace.log(BasicLevel.DEBUG,"");
00411 }
00412 try {
00413 checkContext();
00414 } catch (Exception e) {
00415 trace.log(BasicLevel.ERROR,
00416 "ConnectionImpl.prepareStatement: checkContext error");
00417 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00418 }
00419 if (conman != null) {
00420 return conman.getPStatement(mci, connection, user, sql, t);
00421 } else {
00422 return connection.prepareStatement(sql, t);
00423 }
00424 }
00425
00426
00427 public PreparedStatement prepareStatement(String sql,
00428 String[] t)
00429 throws SQLException {
00430 if (trace.isLoggable(BasicLevel.DEBUG)) {
00431 trace.log(BasicLevel.DEBUG,"");
00432 }
00433 try {
00434 checkContext();
00435 } catch (Exception e) {
00436 trace.log(BasicLevel.ERROR,
00437 "ConnectionImpl.prepareStatement: checkContext error");
00438 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00439 }
00440 if (conman != null) {
00441 return conman.getPStatement(mci, connection, user, sql, t);
00442 } else {
00443 return connection.prepareStatement(sql, t);
00444 }
00445 }
00446
00447
00448 public PreparedStatement prepareStatement(String sql,
00449 int autoGenKeys)
00450 throws SQLException {
00451 if (trace.isLoggable(BasicLevel.DEBUG)) {
00452 trace.log(BasicLevel.DEBUG,"");
00453 }
00454 try {
00455 checkContext();
00456 } catch (Exception e) {
00457 trace.log(BasicLevel.ERROR,
00458 "ConnectionImpl.prepareStatement: checkContext error");
00459 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00460 }
00461 if (conman != null) {
00462 return conman.getPStatement(mci, connection, user, sql, autoGenKeys);
00463 } else {
00464 return connection.prepareStatement(sql, autoGenKeys);
00465 }
00466 }
00467
00468
00469
00470 public CallableStatement prepareCall(String sql,
00471 int rsettype,
00472 int rsetconcurrency,
00473 int holdability)
00474 throws SQLException {
00475 if (trace.isLoggable(BasicLevel.DEBUG)) {
00476 trace.log(BasicLevel.DEBUG,"");
00477 }
00478 try {
00479 checkContext();
00480 } catch (Exception e) {
00481 trace.log(BasicLevel.ERROR,
00482 "ConnectionImpl.prepareStatement: checkContext error");
00483 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00484 }
00485 return connection.prepareCall(sql, rsettype,
00486 rsetconcurrency, holdability);
00487 }
00488
00489
00490 public Statement createStatement(int rsettype,
00491 int rsetconcurrency,
00492 int holdability)
00493 throws SQLException {
00494 if (trace.isLoggable(BasicLevel.DEBUG)) {
00495 trace.log(BasicLevel.DEBUG,"");
00496 }
00497 try {
00498 checkContext();
00499 } catch (Exception e) {
00500 trace.log(BasicLevel.ERROR,
00501 "ConnectionImpl.prepareStatement: checkContext error");
00502 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00503 }
00504 return connection.createStatement(rsettype, rsetconcurrency,
00505 holdability);
00506 }
00507
00508
00509
00510 public void releaseSavepoint(java.sql.Savepoint sp)
00511 throws SQLException {
00512 trace.log(BasicLevel.DEBUG,"");
00513 try {
00514 checkContext();
00515 connection.releaseSavepoint(sp);
00516 } catch (Exception e) {
00517 trace.log(BasicLevel.ERROR,e.getMessage());
00518 e.printStackTrace();
00519 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00520 }
00521 }
00522
00523
00524 public void rollback(java.sql.Savepoint sp) throws SQLException {
00525 if (trace.isLoggable(BasicLevel.DEBUG)) {
00526 trace.log(BasicLevel.DEBUG,"");
00527 }
00528 try {
00529 checkContext();
00530 connection.rollback(sp);
00531 } catch (Exception e) {
00532 trace.log(BasicLevel.ERROR,e.getMessage());
00533 e.printStackTrace();
00534 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00535 }
00536 }
00537
00538
00539 public java.sql.Savepoint setSavepoint()
00540 throws SQLException {
00541 if (trace.isLoggable(BasicLevel.DEBUG)) {
00542 trace.log(BasicLevel.DEBUG,"");
00543 }
00544 try {
00545 checkContext();
00546 return connection.setSavepoint();
00547 } catch (Exception e) {
00548 trace.log(BasicLevel.ERROR,e.getMessage());
00549 e.printStackTrace();
00550 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00551 }
00552 }
00553
00554
00555 public java.sql.Savepoint setSavepoint(String name)
00556 throws SQLException {
00557 if (trace.isLoggable(BasicLevel.DEBUG)) {
00558 trace.log(BasicLevel.DEBUG,"");
00559 }
00560 try {
00561 checkContext();
00562 return connection.setSavepoint(name);
00563 } catch (Exception e) {
00564 trace.log(BasicLevel.ERROR,e.getMessage());
00565 e.printStackTrace();
00566 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00567 }
00568 }
00569
00570 public void setAutoCommit(boolean isauto) throws SQLException {
00571 if (trace.isLoggable(BasicLevel.DEBUG)) {
00572 trace.log(BasicLevel.DEBUG,"");
00573 }
00574 try {
00575 checkContext();
00576 connection.setAutoCommit(isauto);
00577 } catch (Exception e) {
00578 trace.log(BasicLevel.ERROR,"ConnectionImpl.setAC: error");
00579 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00580 }
00581 }
00582
00583 public void setCatalog(String catalog)
00584 throws SQLException {
00585 if (trace.isLoggable(BasicLevel.DEBUG)) {
00586 trace.log(BasicLevel.DEBUG,"");
00587 }
00588 try {
00589 checkContext();
00590 } catch (Exception e) {
00591 trace.log(BasicLevel.ERROR,
00592 "ConnectionImpl.setCatalog: checkContext error");
00593 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00594 }
00595 connection.setCatalog(catalog);
00596 }
00597
00598
00599 public void setHoldability(int holdability)
00600 throws SQLException {
00601 if (trace.isLoggable(BasicLevel.DEBUG)) {
00602 trace.log(BasicLevel.DEBUG,"");
00603 }
00604 try {
00605 checkContext();
00606 } catch (Exception e) {
00607 trace.log(BasicLevel.ERROR,
00608 "ConnectionImpl.setCatalog: checkContext error");
00609 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00610 }
00611 connection.setHoldability(holdability);
00612 }
00613
00614 public void setReadOnly(boolean readOnly)
00615 throws SQLException {
00616 if (trace.isLoggable(BasicLevel.DEBUG)) {
00617 trace.log(BasicLevel.DEBUG,"");
00618 }
00619 try {
00620 checkContext();
00621 } catch (Exception e) {
00622 trace.log(BasicLevel.ERROR,
00623 "ConnectionImpl.setReadOnly: checkContext error");
00624 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00625 }
00626 connection.setReadOnly(readOnly);
00627 }
00628
00629 public void setTransactionIsolation(int level)
00630 throws SQLException {
00631 if (trace.isLoggable(BasicLevel.DEBUG)) {
00632 trace.log(BasicLevel.DEBUG,"");
00633 }
00634 try {
00635 checkContext();
00636 } catch (Exception e) {
00637 trace.log(BasicLevel.ERROR,
00638 "ConnectionImpl.setTransactionIsolation: checkContext error");
00639 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00640 }
00641 connection.setTransactionIsolation(level);
00642 }
00643
00644 public void setTypeMap(Map map)
00645 throws SQLException {
00646 if (trace.isLoggable(BasicLevel.DEBUG)) {
00647 trace.log(BasicLevel.DEBUG,"");
00648 }
00649 try {
00650 checkContext();
00651 } catch (Exception e) {
00652 trace.log(BasicLevel.ERROR,
00653 "ConnectionImpl.setTypeMap: checkContext error");
00654 throw new SQLException("JOnAS JDBC: " + e.getMessage());
00655 }
00656 connection.setTypeMap(map);
00657 }
00658
00659 private void closeConnectionImpl() throws ResourceException {
00660 if (key != 0) {
00661 mc.close(this);
00662 key = 0;
00663 }
00664 }
00665 private void checkContext() throws Exception {
00666 if (key == 0) {
00667 throw new Exception("Connection is closed");
00668 }
00669 if (key != mc.getSignature()) {
00670 if (mc.getSignature() == 0) {
00671 mc.setSignature(key);
00672 } else {
00673 throw new Exception("Connection w/sig("+key+") is not currect active Connection ("+mc.getSignature()+")");
00674 }
00675 }
00676 }
00677
00678 public void setSignature(long sig) {
00679 key = sig;
00680 }
00681 }