英文:
Usertransaction is not working in wildfly with Oracle DB
问题
以下是翻译好的内容:
我认为在 Wildfly 8.2 和 Oracle 12.2 上事务不起作用,以下是我正在尝试运行的代码:
public static void checkTransaction() throws Exception {
Statement stmt = null; // 非事务性语句
Statement stmtx = null;
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource) initialContext.lookup("jdbc/myDataSource");
Connection conn = ds.getConnection("USERNAME", "PASSWORD");
UserTransaction txn = (UserTransaction) new
InitialContext().lookup("java:jboss/UserTransaction");
try {
stmt = conn.createStatement(); // 非事务性语句
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 transaction.");
txn.begin();
stmtx = conn.createStatement(); // 将是事务性语句
stmtx.executeUpdate("INSERT INTO test_table (a, b) VALUES (1,2)");
// 首先,我们尝试回滚更改
txn.rollback(); // 回滚事务
txn.begin(); // 开始第二个事务
stmtx = conn.createStatement();
stmtx.executeUpdate("INSERT INTO test_table (a, b) VALUES (3,4)");
txn.commit(); // 提交第二个事务
} 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>
</datasources>
发生的情况:
根据代码的预期,test_table 应该只有一个条目,即 (3,4),因为之前插入 (1,2) 的事务已被回滚,但实际上表中有 2 行,两个 SQL 语句都被提交。有没有什么原因导致这个事务不起作用?
这是我为此代码所遵循的链接:
我还假设数据源的 jta 默认值为 true,不需要显式添加。
英文:
I believe transaction is not working on Wildfly 8.2 and oracle 12.2 following is the code that I am trying to run:
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();
}
}
The datasource is configured as following:
<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>
</datasources>
What is happenning:
It is expected from the code that the test_table will be having only one entry i.e (3,4) because the previous transaction in which (1,2) is inserted is rolled back but in reality the table contains 2 rows and both the sqls are committed. Any reason why this transaction is not working ?
This is the link that I followed for this code:
I am also assuming that the default value of jta is true in the data-source and that need not be added explicitly.
答案1
得分: 1
我已经能够找出解决这个问题的方法,即针对异步线程的情况。
解决方案是在UserTransaction.begin()完成后从DataSource获取连接。
这与Wildfly文档中的情况不符,在示例中显示可以在Transaction.begin()之前获取连接,但在异步线程的情况下(在Wildfly管理的Bean场景中可以正常工作),这种方法不起作用。
Wildfly事务示例链接:https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6/html/development_guide/jta_transaction_example
英文:
I was able to figure out the solution for this problem i.e. for Asynchronous Threads.
Solution is to fetch the connection from DataSource after UserTransaction.begin() is done.
This is not inline with the Wildfly documentation where the example is shows that connection can be fetched before Transaction.begin() but it doesn't work for Asynchronous Threads scenario (works in Wildfly managed beans scenario flawlessly though).
Link to Wildfly transaction example : https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/6/html/development_guide/jta_transaction_example
答案2
得分: 0
我有一个类似的问题。然而,当我通过Servlet进行访问时,与上述情况类似。问题出现在我尝试在非EE线程中使用事务时。它不会抛出任何异常,但是即使我已经使用了UserTransaction.rollback(),数据库事务仍然被提交。
根据Wildfly 8文档,使用java:jboss/UserTransaction来获取用户事务,该事务应该是存在的,代码确实获得了UserTransaction对象,但实际上无法控制如上所示的数据库更改。
我的理解是,我的非EE线程在某种程度上并不属于Wildfly管理的事务,但在文档中找不到任何相关信息。
这里的任何输入都将是很好的。
英文:
I have a similar issue . However, the above when I hit the via Servlet . The issue comes when I am trying to use transaction in non EE threads . It doesn't throw any exception but DB transaction is still committed even though I have used Usertransaction.rollback().
Per Wildfly 8 documentation using java:jboss/UserTransaction for getting user transaction which should be present and code does get the Usertransaction Object but doesn't actually control the DB changes as shown above.
My sense is that my non EE threads are somehow not part of Wildfly managed transaction but can't any documentation around it.
Any inputs here would be great.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论