1 package in.co.daffodil.db.jdbc; 2 3 import java.sql.*; 4 import com.daffodilwoods.database.resource.*; 5 import java.util.*; 6 import java.io.PrintWriter ; 7 import com.daffodilwoods.daffodildb.server.serversystem.*; 8 import java.util.Locale ; 9 import java.lang.reflect.*; 10 11 69 public class DaffodilDBConnection 70 implements java.sql.Connection { 71 72 private _Connection connection; 73 private boolean close; 74 private SQLWarning sqlWarnings; 75 76 private int transactionIsolationLevel = TRANSACTION_READ_COMMITTED; 77 private boolean readOnly; 78 private boolean autoCommit; 79 private String url; 80 static boolean verbose; 81 private static PrintWriter printWriter; 82 Locale locale; 83 private DatabaseMetaData dataBaseMetaData; 84 boolean statementCached = false; 85 86 HashMap cached_pstmt = new HashMap(); 87 88 public DaffodilDBConnection(boolean isCacheEnabled, String url, 89 _Connection connection) throws SQLException { 90 this(url, connection); 91 statementCached = isCacheEnabled; 92 } 93 94 public DaffodilDBConnection(String url, _Connection connection) throws 95 SQLException { 96 this.connection = connection; 97 this.close = false; 98 this.autoCommit = true; 99 this.url = url; 100 try { 101 String countryString = System.getProperty("country"); 102 String languageString = System.getProperty("language"); 103 if (countryString != null && languageString != null) 104 locale = new Locale (languageString, countryString); 105 else 106 locale = Locale.getDefault(); 107 connection.setTransactionIsolation(transactionIsolationLevel); 108 connection.setAutoCommit(true); 109 } 110 catch (DException ex) { 111 throw ex.getSqlException(getLocale()); 112 } 113 } 114 115 public DaffodilDBConnection(String url, _Connection connection, 116 boolean autoCommit0) throws 117 SQLException { 118 this.connection = connection; 119 this.close = false; 120 this.autoCommit = autoCommit0; 121 this.url = url; 122 try { 123 String countryString = System.getProperty("country"); 124 String languageString = System.getProperty("language"); 125 if (countryString != null && languageString != null) 126 locale = new Locale (languageString, countryString); 127 else 128 locale = Locale.getDefault(); 129 connection.setTransactionIsolation(transactionIsolationLevel); 130 connection.setAutoCommit(autoCommit0); 131 } 132 catch (DException ex) { 133 throw ex.getSqlException(getLocale()); 134 } 135 } 136 137 139 public DaffodilDBConnection(_Connection connection) throws 140 SQLException { 141 this.connection = connection; 142 this.close = false; 143 this.url = url; 144 } 145 146 protected void finalize() { 147 try { 148 if (printWriter != null) { 149 printWriter.flush(); 150 printWriter.close(); 151 } 152 } 153 catch (Exception e) { 154 } 155 } 156 157 172 public synchronized Statement createStatement() throws SQLException { 173 verifyConnection(); 174 return new DaffodilDBStatement(this); 175 } 176 177 206 public synchronized java.sql.PreparedStatement prepareStatement(String sql) throws 207 SQLException { 208 verifyConnection(); 209 if (!statementCached) 210 return new DaffodilDBPreparedStatement(this, sql); 211 Object pstmt = getCachedPreparedStatement(Utilities.calculatehashCode(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY,1)); 212 return pstmt != null ? returnPreparedStatement( (DaffodilDBPreparedStatement) pstmt) : 213 new DaffodilDBPreparedStatement(this, sql); 214 } 215 216 243 public synchronized CallableStatement prepareCall(String sql) throws 244 SQLException { 245 verifyConnection(); 246 return new DaffodilDBCallableStatement(this, sql); 247 } 248 249 264 public synchronized String nativeSQL(String sql) throws SQLException { 265 verifyConnection(); 266 return EscapeParser.parse(sql); 267 } 268 269 297 public synchronized void setAutoCommit(boolean autoCommit) throws 298 SQLException { 299 verifyConnection(); 300 try { 301 if (this.autoCommit != autoCommit) { 302 this.autoCommit = autoCommit; 303 connection.setAutoCommit(autoCommit); 304 } 305 } 306 catch (DException ex) { 307 throw ex.getSqlException(getLocale()); 308 } 309 } 310 311 320 public synchronized boolean getAutoCommit() throws SQLException { 321 verifyConnection(); 322 return autoCommit; 323 } 324 325 336 337 341 342 343 public synchronized void commit() throws SQLException { 344 verifyConnection(); 345 if (autoCommit) { 346 DException dex = new DException("DSE28", null); 347 addWarning(new SQLWarning(dex.getMessage(locale), "01000")); 348 } 349 try { 350 connection.commit(); 351 } 352 catch (DException e) { 353 throw e.getSqlException(getLocale()); 354 } 355 } 356 357 367 368 public synchronized void rollback() throws SQLException { 369 verifyConnection(); 370 try { 371 if (autoCommit) { 372 DException dex = new DException("DSE28", null); 373 addWarning(new SQLWarning(dex.getMessage(locale), "01000")); 374 } 375 connection.rollback(); 376 } 377 catch (DException e) { 378 throw e.getSqlException(getLocale()); 379 } 380 } 381 382 395 public synchronized void close() throws SQLException { 396 if (isClosed()) 397 return; 398 try { 399 try { 400 if (printWriter != null) { 401 printWriter.flush(); 402 printWriter.close(); 403 } 404 } 405 catch (Exception e) { 406 } 407 synchronized (connection) { 408 connection.close(); 409 } 410 connection = null; 411 close = true; 412 System.gc(); 413 Runtime.getRuntime().gc(); 414 } 415 catch (DException e) { 416 throw e.getSqlException(getLocale()); 417 } 418 } 419 420 437 public synchronized boolean isClosed() throws SQLException { 438 return close; 439 } 440 441 442 454 public synchronized DatabaseMetaData getMetaData() throws SQLException { 455 verifyConnection(); 456 if (dataBaseMetaData == null) { 457 try { 458 dataBaseMetaData = new DaffodilDBDatabaseMetaData(this, 459 new DaffodilDBConnection(url, connection.getSystemConnection(), false), 460 url); 461 } 462 catch (DException ex) { 463 throw ex.getSqlException(getLocale()); 464 } 465 } 466 return dataBaseMetaData; 467 } 468 469 480 public synchronized void setReadOnly(boolean readOnly0) throws SQLException { 481 verifyConnection(); 482 483 493 } 494 495 503 public synchronized boolean isReadOnly() throws SQLException { 504 verifyConnection(); 505 return readOnly; 506 } 507 508 521 public synchronized void setCatalog(String catalog) throws SQLException { 522 try { 523 verifyConnection(); 524 connection.setCurrentCatalog(catalog); 525 } 526 catch (DException e) { 527 throw e.getSqlException(getLocale()); 528 } 529 } 530 531 538 public synchronized String getCatalog() throws SQLException { 539 try { 540 verifyConnection(); 541 return connection.getCurrentCatalog(); 542 } 543 catch (DException e) { 544 throw e.getSqlException(getLocale()); 545 } 546 } 547 548 570 public synchronized void setTransactionIsolation(int level) throws 571 SQLException { 572 verifyConnection(); 573 try { 574 if (getTransactionIsolation() != level) { 575 synchronized (cached_pstmt) { 576 cached_pstmt.clear(); 577 } 578 } 579 580 transactionIsolationLevel = level; 581 connection.setTransactionIsolation(level); 582 } 583 catch (DException e) { 584 throw e.getSqlException(getLocale()); 585 } 586 } 587 588 602 public synchronized int getTransactionIsolation() throws SQLException { 603 verifyConnection(); 604 try { 605 return connection.getIsolationLevel(); 606 } 607 catch (DException e) { 608 throw e.getSqlException(getLocale()); 609 } 610 } 611 612 633 public synchronized SQLWarning getWarnings() throws SQLException { 634 verifyConnection(); 635 return sqlWarnings; 636 } 637 638 646 public synchronized void clearWarnings() throws SQLException { 647 verifyConnection(); 648 sqlWarnings = null; 649 } 650 651 652 674 public synchronized Statement createStatement(int resultSetType, 675 int resultSetConcurrency) throws 676 SQLException { 677 verifyConnection(); 678 return new DaffodilDBStatement(this, resultSetType, resultSetConcurrency); 679 } 680 681 707 public synchronized java.sql.PreparedStatement prepareStatement(String sql, 708 int resultSetType, int resultSetConcurrency) throws SQLException { 709 verifyConnection(); 710 if (!statementCached) 711 return new DaffodilDBPreparedStatement(this, sql, resultSetType, 712 resultSetConcurrency); 713 714 Object pstmt = getCachedPreparedStatement(Utilities.calculatehashCode(sql,resultSetType,resultSetConcurrency,1)); 715 return pstmt != null ? returnPreparedStatement( (DaffodilDBPreparedStatement) pstmt) : 716 new DaffodilDBPreparedStatement(this, sql, resultSetType, 717 resultSetConcurrency); 718 } 719 720 744 public synchronized CallableStatement prepareCall(String sql, 745 int resultSetType, 746 int resultSetConcurrency) throws 747 SQLException { 748 verifyConnection(); 749 return new DaffodilDBCallableStatement(this, sql, resultSetType, 750 resultSetConcurrency); 751 } 752 753 765 public synchronized java.util.Map getTypeMap() throws SQLException { 766 return new HashMap(); 767 } 768 769 783 public synchronized void setTypeMap(java.util.Map map) throws SQLException { 784 DException dex = new DException("DSE16", new Object [] {"setTypeMap"}); 785 throw dex.getSqlException(getLocale()); 786 } 787 788 792 793 808 809 810 public synchronized void setHoldability(int holdability) throws SQLException { 811 throw new SQLException("Feature Not Supported", "0A000"); 812 } 813 814 826 827 public synchronized int getHoldability() throws SQLException { 828 throw new SQLException("Feature Not Supported", "0A000"); 829 } 830 831 842 public synchronized Savepoint setSavepoint() throws SQLException { 843 try { 844 String savePointName = connection.setSavePoint(); 845 Class cl = Class.forName("in.co.daffodil.db.jdbc.DaffodilDBSavepoint"); 846 Constructor cons = cl.getConstructor(new Class [] {String .class}); 847 Object obj = cons.newInstance(new Object [] {savePointName}); 848 return (Savepoint) obj; 849 } 850 catch (DException ex) { 851 throw ex.getSqlException(getLocale()); 852 } 853 catch (Exception ex) { 854 return null; 855 } 856 } 857 858 870 public synchronized Savepoint setSavepoint(String savePointName) throws 871 SQLException { 872 try { 873 connection.setSavePoint(savePointName); 874 Class cl = Class.forName("in.co.daffodil.db.jdbc.DaffodilDBSavepoint"); 875 Constructor cons = cl.getConstructor(new Class [] {String .class}); 876 Object obj = cons.newInstance(new Object [] {savePointName}); 877 return (Savepoint) obj; 878 } 879 catch (DException ex) { 880 throw ex.getSqlException(getLocale()); 881 } 882 catch (Exception ex) { 883 return null; 884 } 885 } 886 887 902 public synchronized void rollback(Savepoint savepoint) throws SQLException { 903 try { 904 connection.rollbackSavePoint(savepoint.getSavepointName()); 905 } 906 catch (DException ex) { 907 throw ex.getSqlException(getLocale()); 908 } 909 } 910 911 922 public synchronized void releaseSavepoint(Savepoint savepoint) throws 923 SQLException { 924 try { 925 connection.releaseSavePoint(savepoint.getSavepointName()); 926 } 927 catch (DException ex) { 928 throw ex.getSqlException(getLocale()); 929 } 930 } 931 932 962 public synchronized Statement createStatement(int resultSetType, 963 int resultSetConcurrency, 964 int resultSetHoldability) throws 965 SQLException { 966 verifyConnection(); 967 return new DaffodilDBStatement(this, resultSetType, resultSetConcurrency, 968 resultSetHoldability); 969 } 970 971 1006 public synchronized java.sql.PreparedStatement prepareStatement(String sql, 1007 int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws 1008 SQLException { 1009 verifyConnection(); 1010 1011 if (!statementCached) 1012 return new DaffodilDBPreparedStatement(this, sql, resultSetType, 1013 resultSetConcurrency, 1014 resultSetHoldability); 1015 1016 Object pstmt = getCachedPreparedStatement(Utilities.calculatehashCode(sql,resultSetType,resultSetConcurrency,resultSetHoldability)); 1017 return pstmt != null ?returnPreparedStatement( (DaffodilDBPreparedStatement) pstmt): 1018 new DaffodilDBPreparedStatement(this, sql, resultSetType, 1019 resultSetConcurrency, 1020 resultSetHoldability); 1021 } 1022 1023 1055 public synchronized CallableStatement prepareCall(String sql, 1056 int resultSetType, 1057 int resultSetConcurrency, 1058 int resultSetHoldability) throws 1059 SQLException { 1060 verifyConnection(); 1061 return new DaffodilDBCallableStatement(this, sql, resultSetType, 1062 resultSetConcurrency, 1063 resultSetHoldability); 1064 } 1065 1066 1104 public synchronized java.sql.PreparedStatement prepareStatement(String sql, 1105 int autoGeneratedKeys) throws SQLException { 1106 verifyConnection(); 1107 return new DaffodilDBPreparedStatement(this, sql, autoGeneratedKeys); 1108 } 1109 1110 1149 public synchronized java.sql.PreparedStatement prepareStatement(String sql, 1150 int columnIndexes[]) throws SQLException { 1151 verifyConnection(); 1152 return new DaffodilDBPreparedStatement(this, sql, columnIndexes); 1153 } 1154 1155 1194 public synchronized java.sql.PreparedStatement prepareStatement(String sql, 1195 String columnNames[]) throws SQLException { 1196 verifyConnection(); 1197 return new DaffodilDBPreparedStatement(this, sql, columnNames); 1198 } 1199 1200 1201 1202 public _Connection getServerConnection() { 1203 return connection; 1204 } 1205 1206 void setServerConnection(_Connection con) { 1207 connection = con; 1208 } 1209 1210 public _Connection get_Connection() throws SQLException { 1211 try { 1212 return connection.getSystemConnection(); 1213 } 1214 catch (DException ex) { 1215 throw ex.getSqlException(locale); 1216 } 1217 } 1218 1219 void addWarning(SQLWarning warning) { 1220 if (sqlWarnings == null) 1221 sqlWarnings = warning; 1222 else 1223 sqlWarnings.setNextWarning(warning); 1224 } 1225 1226 static void println(String qry) { 1227 if (verbose) 1228 ; } 1230 1231 1236 static void println(String modifier, String qry) { 1237 if (verbose) 1238 ; } 1240 1241 private final void verifyConnection() throws SQLException { 1242 if (close) { 1243 DException dex = new DException("DSE279", null); 1244 throw dex.getSqlException(getLocale()); 1245 } 1246 } 1247 1248 Locale getLocale() { 1249 return locale; 1250 } 1251 1252 private Object getCachedPreparedStatement(Object key) { 1253 synchronized(cached_pstmt) { 1254 return cached_pstmt.remove(key); 1255 } 1256 } 1257 1258 public void addToCache(Object key, Object pstmt) { 1259 synchronized (cached_pstmt) { 1260 cached_pstmt.put(key, pstmt); 1261 } 1262 } 1263 1264 public boolean isStatementCachedEnabled(){ 1265 return statementCached; 1266 } 1267 1268 public void enableStatementCache() { 1269 statementCached = true; 1270 } 1271 1272 public void disableStatementCache() { 1273 statementCached = false; 1274 synchronized (cached_pstmt) { 1275 cached_pstmt.clear(); 1276 } 1277 } 1278 1279 private java.sql.PreparedStatement returnPreparedStatement(DaffodilDBPreparedStatement pstmt ) throws SQLException{ 1280 pstmt.setConnection(this); 1281 return pstmt; 1282 } 1283 1284 1285} 1286 | Popular Tags |