简体   繁体   中英

JPA (Hibernate) XA Datasource execute Stored Procedure in Transaction

I'm using JBoss 6, JPA (Hibernate) on Sql Server 2005 using XA Transactions. I am able to run queries and stored procedures, but I am trying to run a certain stored procedure in it's own transaction (sort of). Meaning if the stored procedure fails or returns a error, changes made by the stored procedure should be rolledback, but not the changes done by other queries such as entityManager.persist()

I've tried BEGIN TRANSACTION, ROLLBACK etc... in the stored procedure itself, but I get a JDBC error that the transaction does not exist.

How can I accomplish this? Running the stored procedure in it's own transaction? (not sure how)

Thanks for the help

Answering my own question, this method executing the stored procedure was a sub method in a EJB, but it was not part of the business interface, Apparently new transactions are not created in this case (even with REQUIRES_NEW), correct me if I'm wrong.

I ended up moving the method to a different EJB and annotating the method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) and getting the SessionContext via

@Resource
protected SessionContext ctx;

and doing ctx.setRollbackOnly(); when I needed to rollback the stored procedure's changes without rolling back the outer transactions changes.

This works fine for me, but if I got anything wrong, feel free to correct me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM