删除级联失败时JTA不回滚

时间:2012-12-28 13:11:31

标签: jpa transactions ejb cascade jta

使用:Glassfish 3.1.2,EclipseLink。

我有以下三类JPA模型:

@Entity public class Customer implements Serializable {

@Id private Integer id;

@OneToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, orphanRemoval=true)
private Person person;

[...]

@Entity public class Person implements Serializable {

@Id private Integer id;

[...]

@Entity public class Request implements Serializable {

@Id private Integer id;

@ManyToOne private Person person;

我尝试使用以下策略删除客户(使用 CMT ):

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.ddl-generation" value="create-tables"/>
        <property name="eclipselink.ddl-generation.output-mode" value="database"/>

        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.logging.parameters" value="true"/>
        <property name="eclipselink.logging.logger" value="DefaultLogger"/>                  
        <property name="eclipselink.logging.timestamp" value="true"/>
        <property name="eclipselink.logging.session" value="false"/>
        <property name="eclipselink.logging.thread" value="false"/>
    </properties>
</persistence-unit>

[...]

@PersistenceContext(unitName="MyPU")
private EntityManager entityManager;   

@Resource private SessionContext context;

[...]

public void delete(Entity object) {

    try{

        object = this.getEntityManager().merge(object);
        this.getEntityManager().remove(object);

    } catch (Exception e){

        this.context.setRollbackOnly();
    }
}

客户对象附加到附加到请求对象时, Person <的删除级联< / strong>导致事务无法回滚,但数据库中的客户 已删除。我收到以下错误:

INFO: [EL Fine]: 2012-12-28 10:53:38.1--Connection(27132168)--DELETE FROM CUSTOMER WHERE (ID = ?)
bind => [97]
INFO: [EL Fine]: 2012-12-28 10:53:38.125--Connection(27132168)--DELETE FROM PERSON WHERE (ID = ?)
bind => [111]
INFO: [EL Fine]: 2012-12-28 10:53:38.126--SELECT 1
WARNING: DTX5014: Caught exception in beforeCompletion() callback:
Local Exception Stack:
INFO: [EL Warning]: 2012-12-28 10:53:38.127--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: atualização ou exclusão em tabela "person" viola restrição de chave estrangeira "fk_request_person_id" em "request"
Detalhe: Chave (id)=(111) ainda é referenciada pela tabela "request".
Error Code: 0
Call: DELETE FROM PERSON WHERE (ID = ?)
bind => [111]
Query: DeleteObjectQuery(111)
[...]
SEVERE: javax.ejb.EJBException: Transaction aborted
[...]

那么,当级联删除失败时,如何取消客户删除?

1 个答案:

答案 0 :(得分:0)

这里有两件事可能会出错。

  • 未正确指定事务边界

可能由于这个原因,您的应用程序服务器没有正确发出BEGIN语句。这可以解释Postgres有问题,而Oracle没有(它隐含地启动了一个事务)。确保您的服务方法佩戴​​正确的注释。如果一切都很好,也许

  • 您的数据源存在问题。

它是兼容JTA的数据源吗?它是否使用Postgres的正确驱动程序?请发布您的配置,以便我们结账。

我找到了一个可能对你有帮助的有趣链接。这是关于Postgres保持自动提交模式(虽然使用Spring时):

http://archives.postgresql.org/pgsql-jdbc/2007-07/msg00115.php