【发布时间】:2020-08-27 11:47:33
【问题描述】:
我认为事务在 Wildfly 8.2 和 oracle 12.2 上不起作用,以下是我尝试运行的代码:
public static void checkTransaction() throws Exception {
Statement stmt = null; // Non-transactional statement
Statement stmtx = null;
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup("jdbc/myDataSource");
Connection conn = ds.getConnection("USERENAME", "PASSWORD");
UserTransaction txn = (UserTransaction) new
InitialContext().lookup("java:jboss/UserTransaction");
try {
stmt = conn.createStatement(); // non-tx statement
try {
stmt.executeUpdate("DROP TABLE test_table");
} catch (Exception e) {
e.printStackTrace();
}
try {
stmt.executeUpdate("CREATE TABLE test_table (a INTEGER,b INTEGER)");
} catch (Exception e) {
throw new RuntimeException(e);
}
try {
System.out.println("Starting top-level tranasction.");
txn.begin();
stmtx = conn.createStatement(); // will be a tx-statement
stmtx.executeUpdate("INSERT INTO test_table (a, b) VALUES (1,2)");
// First, we try to roll back changes
txn.rollback(); // rollback
txn.begin(); // start second tranaction
stmtx = conn.createStatement();
stmtx.executeUpdate("INSERT INTO test_table (a, b) VALUES (3,4)");
txn.commit(); // committing the second transaction
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} catch (Exception sysEx) {
sysEx.printStackTrace();
}
}
数据源配置如下:
<datasources>
<datasource enabled="true" jndi-name="java:/jdbc/myDataSource" pool-name="DataSourcePool" use-ccm="true">
<connection-url>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=host)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=serviceId)))</connection-url>
<driver>oracle</driver>
<pool>
<min-pool-size>4</min-pool-size>
<max-pool-size>18</max-pool-size>
</pool>
<security>
<security-domain>EncryptedDBPassword</security-domain>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql>
<validate-on-match>true</validate-on-match>
<background-validation>true</background-validation>
<background-validation-millis>30000</background-validation-millis>
<stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
</validation>
<timeout>
<set-tx-query-timeout>true</set-tx-query-timeout>
<blocking-timeout-millis>10000</blocking-timeout-millis>
<idle-timeout-minutes>1</idle-timeout-minutes>
<query-timeout>120</query-timeout>
<use-try-lock>0</use-try-lock>
<allocation-retry>0</allocation-retry>
<allocation-retry-wait-millis>0</allocation-retry-wait-millis>
</timeout>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver module="com.h2database.h2" name="h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver module="com.oracle.ojdbc" name="oracle">
<driver-class>oracle.jdbc.OracleDriver</driver-class>
</driver>
</drivers>
发生了什么:
从代码中预计 test_table 将只有一个条目,即 (3,4),因为插入 (1,2) 的前一个事务被回滚,但实际上该表包含 2 行并且两者sqls 已提交。有什么原因导致此交易无效? 这是我为这段代码所遵循的链接:
我还假设数据源中 jta 的默认值是 true,不需要显式添加。
【问题讨论】:
标签: java oracle jboss wildfly jta