简体   繁体   English

JPA(休眠)XA数据源在事务中执行存储过程

[英]JPA (Hibernate) XA Datasource execute Stored Procedure in Transaction

I'm using JBoss 6, JPA (Hibernate) on Sql Server 2005 using XA Transactions. 我在使用XA事务的Sql Server 2005上使用JBoss 6,JPA(休眠)。 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() 意味着如果存储过程失败或返回错误,则应回滚存储过程所做的更改,而不回退其他查询(例如,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. 我已经在存储过程本身中尝试过BEGIN TRANSACTION,ROLLBACK等...,但是出现了JDBC错误,表明该事务不存在。

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. 回答我自己的问题,这种执行存储过程的方法是EJB中的子方法,但它不是业务接口的一部分,显然在这种情况下不会创建新事务(即使使用REQUIRES_NEW),如果我错误。

I ended up moving the method to a different EJB and annotating the method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) and getting the SessionContext via 我最终将方法移动到另一个EJB,并使用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)注释了该方法,并通过

@Resource
protected SessionContext ctx;

and doing ctx.setRollbackOnly(); 并做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. 这对我来说很好,但是,如果我有任何问题,请随时纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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