1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.jdbc.InternalDriver; 25 26 import org.apache.derby.iapi.reference.Attribute; 27 import org.apache.derby.iapi.reference.JDBC20Translation; 28 import org.apache.derby.iapi.reference.JDBC30Translation; 29 import org.apache.derby.iapi.reference.MessageId; 30 import org.apache.derby.iapi.reference.Property; 31 import org.apache.derby.iapi.reference.SQLState; 32 33 import org.apache.derby.iapi.services.context.ContextManager; 34 import org.apache.derby.iapi.services.memory.LowMemory; 35 import org.apache.derby.iapi.services.monitor.Monitor; 36 import org.apache.derby.iapi.services.sanity.SanityManager; 37 38 import org.apache.derby.iapi.jdbc.AuthenticationService; 39 import org.apache.derby.iapi.jdbc.EngineConnection; 40 41 import org.apache.derby.iapi.db.Database; 42 import org.apache.derby.iapi.error.StandardException; 43 import org.apache.derby.iapi.services.i18n.MessageService; 44 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 45 import org.apache.derby.iapi.sql.execute.ExecutionContext; 46 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 47 import org.apache.derby.iapi.store.access.XATransactionController; 48 49 53 import java.sql.PreparedStatement ; 54 import java.sql.CallableStatement ; 55 import java.sql.DatabaseMetaData ; 56 import java.sql.SQLException ; 57 import java.sql.SQLWarning ; 58 import java.sql.Statement ; 59 60 import java.util.Properties ; 61 62 import org.apache.derby.impl.jdbc.authentication.NoneAuthenticationServiceImpl; 63 64 94 public class EmbedConnection implements EngineConnection 95 { 96 97 private static final StandardException exceptionClose = StandardException.closeException(); 98 99 106 public static final SQLException NO_MEM = 107 Util.generateCsSQLException(SQLState.LOGIN_FAILED, "java.lang.OutOfMemoryError"); 108 109 112 public static final LowMemory memoryState = new LowMemory(); 113 114 DatabaseMetaData dbMetadata; 118 119 final TransactionResourceImpl tr; 121 122 private boolean active; 127 boolean autoCommit = true; 128 boolean needCommit; 129 130 private boolean usingNoneAuth; 132 133 139 private int connectionHoldAbility = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; 140 141 142 151 final EmbedConnection rootConnection; 152 private SQLWarning topWarning; 153 156 private InternalDriver factory; 157 158 165 private java.sql.Connection applicationConnection; 166 167 172 private int resultSetId; 173 174 175 private String connString; 176 177 178 182 public EmbedConnection(InternalDriver driver, String url, Properties info) 185 throws SQLException 186 { 187 applicationConnection = rootConnection = this; 189 factory = driver; 190 191 192 tr = new TransactionResourceImpl(driver, url, info); 193 194 active = true; 195 196 setupContextStack(); 199 200 try { 201 202 EmbedConnectionContext context = pushConnectionContext(tr.getContextManager()); 204 205 boolean shutdown = Boolean.valueOf(info.getProperty(Attribute.SHUTDOWN_ATTR)).booleanValue(); 207 208 Database database = (Database) Monitor.findService(Property.DATABASE_MODULE, tr.getDBName()); 210 211 boolean createBoot = createBoot(info); 213 if (database != null) 214 { 215 tr.setDatabase(database); 217 } 218 else if (!shutdown) 219 { 220 if (!bootDatabase(info)) 224 { 225 tr.clearContextInError(); 226 setInactive(); 227 return; 228 } 229 } 230 231 232 if (createBoot && !shutdown) 233 { 234 236 if (tr.getDatabase() != null) { 237 addWarning(EmbedSQLWarning.newEmbedSQLWarning(SQLState.DATABASE_EXISTS, getDBName())); 238 } else { 239 240 checkUserCredentials(null, info); 248 249 database = createDatabase(tr.getDBName(), info); 251 tr.setDatabase(database); 252 } 253 } 254 255 256 if (tr.getDatabase() == null) { 257 String dbname = tr.getDBName(); 258 this.setInactive(); 261 throw newSQLException(SQLState.DATABASE_NOT_FOUND, dbname); 262 } 263 264 265 checkUserCredentials(tr.getDBName(), info); 269 270 tr.startTransaction(); 273 274 if (shutdown) { 276 throw tr.shutdownDatabaseException(); 277 } 278 279 if (usingNoneAuth && getLanguageConnection().usesSqlAuthorization()) 281 addWarning(EmbedSQLWarning.newEmbedSQLWarning(SQLState.SQL_AUTHORIZATION_WITH_NO_AUTHENTICATION)); 282 } 283 catch (OutOfMemoryError noMemory) 284 { 285 restoreContextStack(); 287 tr.lcc = null; 288 tr.cm = null; 289 290 memoryState.setLowMemory(); 293 294 throw NO_MEM; 297 } 298 catch (Throwable t) { 299 throw handleException(t); 300 } finally { 301 restoreContextStack(); 302 } 303 } 304 305 306 324 private boolean createBoot(Properties p) throws SQLException 325 { 326 int createCount = 0; 327 328 if (Boolean.valueOf(p.getProperty(Attribute.CREATE_ATTR)).booleanValue()) 329 createCount++; 330 331 int restoreCount=0; 332 if (p.getProperty(Attribute.CREATE_FROM) != null) 334 restoreCount++; 335 if (p.getProperty(Attribute.RESTORE_FROM) != null) 336 restoreCount++; 337 if (p.getProperty(Attribute.ROLL_FORWARD_RECOVERY_FROM)!=null) 338 restoreCount++; 339 if(restoreCount > 1) 340 throw newSQLException(SQLState.CONFLICTING_RESTORE_ATTRIBUTES); 341 342 if (restoreCount != 0 && 346 (Boolean.valueOf(p.getProperty( 347 Attribute.DATA_ENCRYPTION)).booleanValue() || 348 p.getProperty(Attribute.NEW_BOOT_PASSWORD) != null || 349 p.getProperty(Attribute.NEW_CRYPTO_EXTERNAL_KEY) != null 350 )) 351 { 352 throw newSQLException(SQLState.CONFLICTING_RESTORE_ATTRIBUTES); 353 } 354 355 356 createCount = createCount + restoreCount ; 359 360 if (createCount > 1) throw newSQLException(SQLState.CONFLICTING_CREATE_ATTRIBUTES); 362 363 return (createCount - restoreCount) == 1; 365 } 366 367 376 public EmbedConnection(EmbedConnection inputConnection) 377 { 378 if (SanityManager.DEBUG) 379 { 380 SanityManager.ASSERT(inputConnection.active, 381 "trying to create a proxy for an inactive conneciton"); 382 } 383 384 autoCommit = false; 387 388 389 393 394 397 tr = null; active = true; 402 this.rootConnection = inputConnection.rootConnection; 403 this.applicationConnection = this; 404 this.factory = inputConnection.factory; 405 406 this.connectionHoldAbility = inputConnection.connectionHoldAbility; 409 410 } 419 420 private void checkUserCredentials(String dbname, 424 Properties userInfo) 425 throws SQLException 426 { 427 if (SanityManager.DEBUG) 428 SanityManager.ASSERT(!isClosed(), "connection is closed"); 429 430 AuthenticationService authenticationService = null; 438 439 if (dbname == null) 441 authenticationService = getLocalDriver().getAuthenticationService(); 442 else 443 authenticationService = getTR().getDatabase().getAuthenticationService(); 444 445 if (authenticationService == null) 448 { 449 String failedString = MessageService.getTextMessage( 450 (dbname == null) ? MessageId.AUTH_NO_SERVICE_FOR_SYSTEM : MessageId.AUTH_NO_SERVICE_FOR_DB); 451 452 throw newSQLException(SQLState.LOGIN_FAILED, failedString); 453 } 454 455 457 if (!authenticationService.authenticate( 458 dbname, 459 userInfo 460 )) { 461 462 throw newSQLException(SQLState.LOGIN_FAILED, MessageService.getTextMessage(MessageId.AUTH_INVALID)); 463 464 } 465 466 if (authenticationService instanceof NoneAuthenticationServiceImpl) 470 usingNoneAuth = true; 471 } 472 473 478 public int getEngineType() 479 { 480 Database db = getDatabase(); 481 482 if( null == db) 483 return 0; 484 return db.getEngineType(); 485 } 486 487 490 491 505 public final Statement createStatement() throws SQLException 506 { 507 return createStatement(JDBC20Translation.TYPE_FORWARD_ONLY, 508 JDBC20Translation.CONCUR_READ_ONLY, 509 connectionHoldAbility); 510 } 511 512 523 public final Statement createStatement(int resultSetType, 524 int resultSetConcurrency) 525 throws SQLException 526 { 527 return createStatement(resultSetType, resultSetConcurrency, 528 connectionHoldAbility); 529 } 530 531 545 public final Statement createStatement(int resultSetType, 546 int resultSetConcurrency, 547 int resultSetHoldability) 548 throws SQLException 549 { 550 checkIfClosed(); 551 552 return factory.newEmbedStatement(this, false, 553 setResultSetType(resultSetType), resultSetConcurrency, 554 resultSetHoldability); 555 } 556 557 583 public final PreparedStatement prepareStatement(String sql) 584 throws SQLException 585 { 586 return prepareStatement(sql,JDBC20Translation.TYPE_FORWARD_ONLY, 587 JDBC20Translation.CONCUR_READ_ONLY, 588 connectionHoldAbility, 589 JDBC30Translation.NO_GENERATED_KEYS, 590 null, 591 null); 592 } 593 594 595 607 public final PreparedStatement prepareStatement(String sql, int resultSetType, 608 int resultSetConcurrency) 609 throws SQLException 610 { 611 return prepareStatement(sql, 612 resultSetType, 613 resultSetConcurrency, 614 connectionHoldAbility, 615 JDBC30Translation.NO_GENERATED_KEYS, 616 null, 617 null); 618 } 619 620 635 public final PreparedStatement prepareStatement(String sql, int resultSetType, 636 int resultSetConcurrency, int resultSetHoldability) 637 throws SQLException 638 { 639 return prepareStatement(sql, 640 resultSetType, 641 resultSetConcurrency, 642 resultSetHoldability, 643 JDBC30Translation.NO_GENERATED_KEYS, 644 null, 645 null); 646 } 647 648 649 669 public final PreparedStatement prepareStatement( 670 String sql, 671 int[] columnIndexes) 672 throws SQLException 673 { 674 throw Util.notImplemented("prepareStatement(String, int[])"); 675 } 676 677 696 public final PreparedStatement prepareStatement( 697 String sql, 698 String [] columnNames) 699 throws SQLException 700 { 701 throw Util.notImplemented("prepareStatement(String, String[])"); 702 } 703 704 720 public final PreparedStatement prepareStatement( 721 String sql, 722 int autoGeneratedKeys) 723 throws SQLException 724 { 725 return prepareStatement(sql, 726 JDBC20Translation.TYPE_FORWARD_ONLY, 727 JDBC20Translation.CONCUR_READ_ONLY, 728 connectionHoldAbility, 729 autoGeneratedKeys, 730 null, 731 null); 732 } 733 734 private PreparedStatement prepareStatement(String sql, int resultSetType, 735 int resultSetConcurrency, int resultSetHoldability, 736 int autoGeneratedKeys, int[] columnIndexes, String [] columnNames) 737 throws SQLException 738 { 739 synchronized (getConnectionSynchronization()) { 740 setupContextStack(); 741 try { 742 return factory.newEmbedPreparedStatement(this, sql, false, 743 setResultSetType(resultSetType), 744 resultSetConcurrency, 745 resultSetHoldability, 746 autoGeneratedKeys, 747 columnIndexes, 748 columnNames); 749 } finally { 750 restoreContextStack(); 751 } 752 } 753 } 754 755 780 public final CallableStatement prepareCall(String sql) 781 throws SQLException 782 { 783 return prepareCall(sql, JDBC20Translation.TYPE_FORWARD_ONLY, 784 JDBC20Translation.CONCUR_READ_ONLY, 785 connectionHoldAbility); 786 } 787 788 800 public final CallableStatement prepareCall(String sql, int resultSetType, 801 int resultSetConcurrency) 802 throws SQLException 803 { 804 return prepareCall(sql, resultSetType, resultSetConcurrency, 805 connectionHoldAbility); 806 } 807 808 823 public final CallableStatement prepareCall(String sql, int resultSetType, 824 int resultSetConcurrency, int resultSetHoldability) 825 throws SQLException 826 { 827 checkIfClosed(); 828 829 synchronized (getConnectionSynchronization()) 830 { 831 setupContextStack(); 832 try 833 { 834 return factory.newEmbedCallableStatement(this, sql, 835 setResultSetType(resultSetType), 836 resultSetConcurrency, 837 resultSetHoldability); 838 } 839 finally 840 { 841 restoreContextStack(); 842 } 843 } 844 } 845 846 855 public String nativeSQL(String sql) throws SQLException { 856 checkIfClosed(); 857 return sql; 859 } 860 861 882 public void setAutoCommit(boolean autoCommit) throws SQLException { 883 checkIfClosed(); 884 885 if (rootConnection != this) { 887 if (autoCommit) 888 throw newSQLException(SQLState.NO_AUTO_COMMIT_ON); 889 } 890 891 if (this.autoCommit != autoCommit) 892 commit(); 893 894 this.autoCommit = autoCommit; 895 } 896 897 903 public boolean getAutoCommit() throws SQLException { 904 checkIfClosed(); 905 return autoCommit; 906 } 907 908 917 public void commit() throws SQLException { 918 synchronized (getConnectionSynchronization()) 919 { 920 924 setupContextStack(); 925 926 try 927 { 928 getTR().commit(); 929 } 930 catch (Throwable t) 931 { 932 throw handleException(t); 933 } 934 finally 935 { 936 restoreContextStack(); 937 } 938 939 needCommit = false; 940 } 941 } 942 943 952 public void rollback() throws SQLException { 953 954 synchronized (getConnectionSynchronization()) 955 { 956 960 setupContextStack(); 961 try 962 { 963 getTR().rollback(); 964 } catch (Throwable t) { 965 throw handleException(t); 966 } 967 finally 968 { 969 restoreContextStack(); 970 } 971 needCommit = false; 972 } 973 } 974 975 987 public void close() throws SQLException { 988 if (isClosed()) 990 return; 991 992 993 if (rootConnection == this) 994 { 995 996 if (!autoCommit && !transactionIsIdle()) { 997 throw newSQLException(SQLState.LANG_INVALID_TRANSACTION_STATE); 998 } 999 1000 close(exceptionClose); 1001 } 1002 else 1003 setInactive(); } 1005 1006 private void close(StandardException e) throws SQLException { 1013 1014 synchronized(getConnectionSynchronization()) 1015 { 1016 if (rootConnection == this) 1017 { 1018 1021 if (active) { 1022 setupContextStack(); 1023 try { 1024 tr.rollback(); 1025 1026 tr.clearLcc(); 1032 tr.cleanupOnError(e); 1033 1034 } catch (Throwable t) { 1035 throw handleException(t); 1036 } finally { 1037 restoreContextStack(); 1038 } 1039 } 1040 } 1041 1042 if (!isClosed()) 1043 setInactive(); 1044 } 1045 } 1046 1047 1052 public final boolean isClosed() { 1053 if (active) { 1054 1055 if (getTR().isActive()) { 1057 return false; 1058 } 1059 1060 setInactive(); 1061 1062 } 1063 return true; 1064 } 1065 1066 1076 public DatabaseMetaData getMetaData() throws SQLException { 1077 checkIfClosed(); 1078 1079 if (dbMetadata == null) { 1080 1081 dbMetadata = factory.newEmbedDatabaseMetaData(this, getTR().getUrl()); 1087 } 1088 return dbMetadata; 1089 } 1090 1091 1101 public final int getHoldability() throws SQLException { 1102 checkIfClosed(); 1103 return connectionHoldAbility; 1104 } 1105 1106 1116 public final void setHoldability(int holdability) throws SQLException { 1117 checkIfClosed(); 1118 connectionHoldAbility = holdability; 1119 } 1120 1121 1132 public final void setReadOnly(boolean readOnly) throws SQLException 1133 { 1134 synchronized(getConnectionSynchronization()) 1135 { 1136 setupContextStack(); 1137 try { 1138 getLanguageConnection().setReadOnly(readOnly); 1139 } catch (StandardException e) { 1140 throw handleException(e); 1141 } finally { 1142 restoreContextStack(); 1143 } 1144 } 1145 } 1146 1147 1153 public final boolean isReadOnly() throws SQLException 1154 { 1155 checkIfClosed(); 1156 return getLanguageConnection().isReadOnly(); 1157 } 1158 1159 1166 public void setCatalog(String catalog) throws SQLException { 1167 checkIfClosed(); 1168 return; 1170 } 1171 1172 1178 public String getCatalog() throws SQLException { 1179 checkIfClosed(); 1180 return null; 1183 } 1184 1185 1198 public void setTransactionIsolation(int level) throws SQLException { 1199 1200 if (level == getTransactionIsolation()) 1201 return; 1202 1203 int iLevel; 1205 switch (level) 1206 { 1207 case java.sql.Connection.TRANSACTION_READ_UNCOMMITTED: 1208 iLevel = ExecutionContext.READ_UNCOMMITTED_ISOLATION_LEVEL; 1209 break; 1210 1211 case java.sql.Connection.TRANSACTION_READ_COMMITTED: 1212 iLevel = ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL; 1213 break; 1214 1215 case java.sql.Connection.TRANSACTION_REPEATABLE_READ: 1216 iLevel = ExecutionContext.REPEATABLE_READ_ISOLATION_LEVEL; 1217 break; 1218 1219 case java.sql.Connection.TRANSACTION_SERIALIZABLE: 1220 iLevel = ExecutionContext.SERIALIZABLE_ISOLATION_LEVEL; 1221 break; 1222 default: 1223 throw newSQLException(SQLState.UNIMPLEMENTED_ISOLATION_LEVEL, new Integer (level)); 1224 } 1225 1226 synchronized(getConnectionSynchronization()) 1227 { 1228 setupContextStack(); 1229 try { 1230 getLanguageConnection().setIsolationLevel(iLevel); 1231 } catch (StandardException e) { 1232 throw handleException(e); 1233 } finally { 1234 restoreContextStack(); 1235 } 1236 } 1237 } 1238 1239 1240 1246 public final int getTransactionIsolation() throws SQLException { 1247 checkIfClosed(); 1248 return ExecutionContext.CS_TO_JDBC_ISOLATION_LEVEL_MAP[getLanguageConnection().getCurrentIsolationLevel()]; 1249 } 1250 1251 1263 public final synchronized SQLWarning getWarnings() throws SQLException { 1264 checkIfClosed(); 1265 return topWarning; 1266 } 1267 1268 1275 public final synchronized void clearWarnings() throws SQLException { 1276 checkIfClosed(); 1277 topWarning = null; 1278 } 1279 1280 1286 1293 public java.util.Map getTypeMap() throws SQLException { 1294 checkIfClosed(); 1295 return java.util.Collections.EMPTY_MAP; 1297 } 1298 1299 1306 public final void setTypeMap(java.util.Map map) throws SQLException { 1307 checkIfClosed(); 1308 if( map == null) 1309 throw Util.generateCsSQLException(SQLState.INVALID_API_PARAMETER,map,"map", 1310 "java.sql.Connection.setTypeMap"); 1311 if(!(map.isEmpty())) 1312 throw Util.notImplemented(); 1313 } 1314 1315 1321 1328 public final synchronized void addWarning(SQLWarning newWarning) { 1329 if (topWarning == null) { 1330 topWarning = newWarning; 1331 return; 1332 } 1333 1334 topWarning.setNextWarning(newWarning); 1335 } 1336 1337 1342 public String getDBName() 1343 { 1344 if (SanityManager.DEBUG) 1345 SanityManager.ASSERT(!isClosed(), "connection is closed"); 1346 1347 return getTR().getDBName(); 1348 } 1349 1350 public final LanguageConnectionContext getLanguageConnection() { 1351 1352 if (SanityManager.DEBUG) 1353 SanityManager.ASSERT(!isClosed(), "connection is closed"); 1354 1355 return getTR().getLcc(); 1356 } 1357 1358 1363 protected final void checkIfClosed() throws SQLException { 1364 if (isClosed()) { 1365 throw Util.noCurrentConnection(); 1366 } 1367 } 1368 1369 SQLException handleException(Throwable thrownException) 1372 throws SQLException 1373 { 1374 1378 return getTR().handleException(thrownException, 1379 autoCommit, 1380 true ); 1382 } 1383 1384 1406 final SQLException handleException(Throwable thrownException, 1407 boolean rollbackOnAutoCommit) 1408 throws SQLException 1409 { 1410 return getTR().handleException(thrownException, autoCommit, 1411 rollbackOnAutoCommit); 1412 1413 } 1414 1415 1421 1422 1433 1434 public final void setInactive() { 1435 1436 if (active == false) 1437 return; 1438 1441 synchronized (getConnectionSynchronization()) { 1442 active = false; 1443 dbMetadata = null; 1446 } 1447 } 1448 1449 1452 protected void finalize() throws Throwable 1453 { 1454 if (rootConnection == this) 1455 { 1456 super.finalize(); 1457 if (!isClosed()) 1458 close(exceptionClose); 1459 } 1460 } 1461 1462 1466 protected void needCommit() { 1467 if (!needCommit) needCommit = true; 1468 } 1469 1470 1477 protected void commitIfNeeded() throws SQLException 1478 { 1479 if (autoCommit && needCommit) 1480 { 1481 try 1482 { 1483 getTR().commit(); 1484 } 1485 catch (Throwable t) 1486 { 1487 throw handleException(t); 1488 } 1489 needCommit = false; 1490 } 1491 } 1492 1493 1508 protected void commitIfAutoCommit() throws SQLException 1509 { 1510 if (autoCommit) 1511 { 1512 try 1513 { 1514 getTR().commit(); 1515 } 1516 catch (Throwable t) 1517 { 1518 throw handleException(t); 1519 } 1520 needCommit = false; 1521 } 1522 } 1523 1524 1525 final protected Object getConnectionSynchronization() 1526 { 1527 return rootConnection; 1528 } 1529 1530 1534 protected final void setupContextStack() throws SQLException { 1535 1536 1542 1543 checkIfClosed(); 1544 1545 getTR().setupContextStack(); 1546 1547 } 1548 1549 protected final void restoreContextStack() throws SQLException { 1550 1551 if (SanityManager.DEBUG) 1552 Util.ASSERT(this, (active) || getTR().getCsf() !=null, "No context service to do restore"); 1553 1554 TransactionResourceImpl tr = getTR(); 1555 1556 if (SanityManager.DEBUG) 1559 { 1560 if ((tr.getCsf() != null) && (tr.getCsf().getCurrentContextManager() != 1561 tr.getContextManager())) 1562 { 1563 Util.THROWASSERT(this, 1564 "Current Context Manager not the one was expected: " + 1565 tr.getCsf().getCurrentContextManager() + " " + 1566 tr.getContextManager()); 1567 } 1568 } 1569 1570 tr.restoreContextStack(); 1571 } 1572 1573 1576 1577 1586 1587 private Database createDatabase(String dbname, Properties info) 1588 throws SQLException { 1589 1590 info = filterProperties(info); 1591 1592 try { 1593 if (Monitor.createPersistentService(Property.DATABASE_MODULE, dbname, info) == null) 1594 { 1595 addWarning(EmbedSQLWarning.newEmbedSQLWarning(SQLState.DATABASE_EXISTS, dbname)); 1597 } 1598 } catch (StandardException mse) { 1599 1600 SQLException se = newSQLException(SQLState.CREATE_DATABASE_FAILED, dbname); 1601 se.setNextException(handleException(mse)); 1602 throw se; 1603 } 1604 1605 info.clear(); 1609 1610 return (Database) Monitor.findService(Property.DATABASE_MODULE, dbname); 1611 } 1612 1613 1614 1621 1622 private boolean bootDatabase(Properties info) throws Throwable 1623 { 1624 String dbname = tr.getDBName(); 1625 1626 try { 1628 1629 info = filterProperties(info); 1630 1631 if (!Monitor.startPersistentService(dbname, info)) { 1633 return false; 1638 } 1639 1640 info.clear(); 1644 1645 Database database = (Database) Monitor.findService(Property.DATABASE_MODULE, dbname); 1646 tr.setDatabase(database); 1647 1648 } catch (StandardException mse) { 1649 SQLException se = newSQLException(SQLState.BOOT_DATABASE_FAILED, dbname); 1650 1651 Throwable ne = mse.getNestedException(); 1652 SQLException nse; 1653 1654 1665 if (ne instanceof StandardException) 1666 nse = Util.generateCsSQLException((StandardException)ne); 1667 else if (ne != null) 1668 nse = Util.javaException(ne); 1669 else 1670 nse = Util.generateCsSQLException(mse); 1671 1672 se.setNextException(nse); 1673 throw se; 1674 } 1675 1676 return true; 1680 1681 } 1682 1683 1687 1688 PreparedStatement prepareMetaDataStatement(String sql) 1689 throws SQLException { 1690 synchronized (getConnectionSynchronization()) { 1691 setupContextStack(); 1692 PreparedStatement s = null; 1693 try { 1694 s = factory.newEmbedPreparedStatement(this, sql, true, 1695 JDBC20Translation.TYPE_FORWARD_ONLY, 1696 JDBC20Translation.CONCUR_READ_ONLY, 1697 connectionHoldAbility, 1698 JDBC30Translation.NO_GENERATED_KEYS, 1699 null, 1700 null); 1701 } finally { 1702 restoreContextStack(); 1703 } 1704 return s; 1705 } 1706 } 1707 1708 public final InternalDriver getLocalDriver() 1709 { 1710 if (SanityManager.DEBUG) 1711 SanityManager.ASSERT(!isClosed(), "connection is closed"); 1712 1713 return getTR().getDriver(); 1714 } 1715 1716 1719 public final ContextManager getContextManager() { 1720 1721 if (SanityManager.DEBUG) 1722 SanityManager.ASSERT(!isClosed(), "connection is closed"); 1723 1724 return getTR().getContextManager(); 1725 } 1726 1727 1737 private Properties filterProperties(Properties inputSet) { 1738 Properties limited = new Properties (); 1739 1740 for (java.util.Enumeration e = inputSet.propertyNames(); e.hasMoreElements(); ) { 1743 1744 String key = (String ) e.nextElement(); 1745 1746 if (key.startsWith("derby.")) 1748 continue; 1749 limited.put(key, inputSet.getProperty(key)); 1750 } 1751 return limited; 1752 } 1753 1754 1758 1759 protected Database getDatabase() 1760 { 1761 if (SanityManager.DEBUG) 1762 SanityManager.ASSERT(!isClosed(), "connection is closed"); 1763 1764 return getTR().getDatabase(); 1765 } 1766 1767 final protected TransactionResourceImpl getTR() 1768 { 1769 return rootConnection.tr; 1770 } 1771 1772 private EmbedConnectionContext pushConnectionContext(ContextManager cm) { 1773 return new EmbedConnectionContext(cm, this); 1774 } 1775 1776 public final void setApplicationConnection(java.sql.Connection applicationConnection) { 1777 this.applicationConnection = applicationConnection; 1778 } 1779 1780 public final java.sql.Connection getApplicationConnection() { 1781 return applicationConnection; 1782 } 1783 1784 public void setDrdaID(String drdaID) { 1785 getLanguageConnection().setDrdaID(drdaID); 1786 } 1787 1788 1796 public void resetFromPool() throws SQLException { 1797 synchronized (getConnectionSynchronization()) 1798 { 1799 setupContextStack(); 1800 try { 1801 getLanguageConnection().resetFromPool(); 1802 } catch (StandardException t) { 1803 throw handleException(t); 1804 } 1805 finally 1806 { 1807 restoreContextStack(); 1808 } 1809 } 1810 } 1811 1812 1819 1820 1821 1824 1825 public final int xa_prepare() throws SQLException { 1826 1827 synchronized (getConnectionSynchronization()) 1828 { 1829 setupContextStack(); 1830 try 1831 { 1832 XATransactionController tc = 1833 (XATransactionController) getLanguageConnection().getTransactionExecute(); 1834 1835 int ret = tc.xa_prepare(); 1836 1837 if (ret == XATransactionController.XA_RDONLY) 1838 { 1839 1848 getLanguageConnection().internalCommit(false ); 1849 } 1850 return ret; 1851 } catch (StandardException t) 1852 { 1853 throw handleException(t); 1854 } 1855 finally 1856 { 1857 restoreContextStack(); 1858 } 1859 } 1860 } 1861 1862 1863 public final void xa_commit(boolean onePhase) throws SQLException { 1864 1865 synchronized (getConnectionSynchronization()) 1866 { 1867 setupContextStack(); 1868 try 1869 { 1870 getLanguageConnection().xaCommit(onePhase); 1871 } catch (StandardException t) 1872 { 1873 throw handleException(t); 1874 } 1875 finally 1876 { 1877 restoreContextStack(); 1878 } 1879 } 1880 } 1881 public final void xa_rollback() throws SQLException { 1882 1883 synchronized (getConnectionSynchronization()) 1884 { 1885 setupContextStack(); 1886 try 1887 { 1888 getLanguageConnection().xaRollback(); 1889 } catch (StandardException t) 1890 { 1891 throw handleException(t); 1892 } 1893 finally 1894 { 1895 restoreContextStack(); 1896 } 1897 } 1898 } 1899 1904 public final boolean transactionIsIdle() 1905 { 1906 return getTR().isIdle(); 1907 } 1908 private int setResultSetType(int resultSetType) { 1909 1910 1913 if (resultSetType == JDBC20Translation.TYPE_SCROLL_SENSITIVE) 1914 { 1915 addWarning(EmbedSQLWarning.newEmbedSQLWarning(SQLState.NO_SCROLL_SENSITIVE_CURSORS)); 1916 resultSetType = JDBC20Translation.TYPE_SCROLL_INSENSITIVE; 1917 } 1918 return resultSetType; 1919 } 1920 1921 1922 1934 public void setPrepareIsolation(int level) throws SQLException 1935 { 1936 if (level == getPrepareIsolation()) 1937 return; 1938 1939 switch (level) 1940 { 1941 case ExecutionContext.READ_UNCOMMITTED_ISOLATION_LEVEL: 1942 case ExecutionContext.REPEATABLE_READ_ISOLATION_LEVEL: 1943 case ExecutionContext.READ_COMMITTED_ISOLATION_LEVEL: 1944 case ExecutionContext.SERIALIZABLE_ISOLATION_LEVEL: 1945 case ExecutionContext.UNSPECIFIED_ISOLATION_LEVEL: 1946 break; 1947 default: 1948 throw Util.generateCsSQLException( 1949 SQLState.UNIMPLEMENTED_ISOLATION_LEVEL, new Integer (level)); 1950 } 1951 1952 synchronized(getConnectionSynchronization()) 1953 { 1954 getLanguageConnection().setPrepareIsolationLevel(level); 1955 } 1956 } 1957 1958 1961 public int getPrepareIsolation() 1962 { 1963 return getLanguageConnection().getPrepareIsolationLevel(); 1964 } 1965 1966 1972 final int getResultSetOrderId() { 1973 1974 if (this == rootConnection) { 1975 return 0; 1976 } else { 1977 return rootConnection.resultSetId++; 1978 } 1979 } 1980 1981 protected SQLException newSQLException(String messageId) { 1982 return Util.generateCsSQLException(messageId); 1983 } 1984 protected SQLException newSQLException(String messageId, Object arg1) { 1985 return Util.generateCsSQLException(messageId, arg1); 1986 } 1987 protected SQLException newSQLException(String messageId, Object arg1, Object arg2) { 1988 return Util.generateCsSQLException(messageId, arg1, arg2); 1989 } 1990 1991 1997 2011 public String toString() 2012 { 2013 if ( connString == null ) 2014 { 2015 2016 LanguageConnectionContext lcc = getLanguageConnection(); 2017 2018 connString = 2019 this.getClass().getName() + "@" + this.hashCode() + " " + 2020 lcc.xidStr + 2021 lcc.getTransactionExecute().getTransactionIdString() + 2022 "), " + 2023 lcc.lccStr + 2024 Integer.toString(lcc.getInstanceNumber()) + "), " + 2025 lcc.dbnameStr + lcc.getDbname() + "), " + 2026 lcc.drdaStr + lcc.getDrdaID() + ") "; 2027 } 2028 2029 return connString; 2030 } 2031 2032 2033} 2034 | Popular Tags |