Announcement Announcement Module
Collapse
No announcement yet.
JPA + @Transactional - After Rollback, SQL Querys seems to be pendent? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA + @Transactional - After Rollback, SQL Querys seems to be pendent?

    Hi all!
    Im using Spring 3.0.5 for dependency injection, and Transaction Support.

    Code:
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="jpaVendorAdapter">
                    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                        <property name="showSql" value="false" />
                        <property name="generateDdl" value="false" />
                        <!-- Dialeto Oracle -->
                        <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> 
                    </bean>
                </property>
                <property name="jpaProperties">
                <props>
                	<prop key="hibernate.format_sql">true</prop>
                	<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                </props>
                </property>
           </bean>
    
           <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
               <property name="entityManagerFactory" ref="entityManagerFactory" />
           </bean>
    		
           <tx:annotation-driven />
    In my abstract DAO Class, I use this code:

    Code:
    @Transactional(propagation=Propagation.REQUIRED)
        public void deleteById(T entityId) throws DataAccessException {
            if(entityId != null) {
                E entity = findById(entityId);
                if(entity != null) {
                    getEntityManager().remove(entity);
                }
            }
        }
        
        @Transactional(propagation=Propagation.REQUIRED)
        @SuppressWarnings("unchecked")
        public void delete(E entity) {
            if(getEntityManager().contains(entity)) {
                getEntityManager().remove(entity);
            }
            else {
                deleteById( ((DefaultEntity<T>)entity).getId() );
            }
        }
    This works fine. BUT, if try for example, delete an entity that haves a Foreign Key Constraint, course, I got a Exception Throwed:

    Code:
    Caused by: javax.persistence.RollbackException: Error while committing the transaction
    	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93)
    	at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerSynchronization.afterCommit(ExtendedEntityManagerCreator.java:478)
    	... 39 more
    Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.simus.supera.ciclonev2.persistence.atividade.AtividadeConferenciaPrecoModelo#56]
    	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
    	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
    	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
    	... 40 more
    Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.simus.supera.ciclonev2.persistence.atividade.AtividadeConferenciaPrecoModelo#56]
    	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
    	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    	at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2728)
    	at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2911)
    	at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
    	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
    	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:189)
    	at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    	at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    	at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    	at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    	at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76)
    	... 40 more
    The problem, is after that, ALWAYS that I try to persist any entities of the same type, after the Update, or INSERT statement, the system try again, to delete the entity that haves throwed the exception by the constraint.

    SQL Statements logged:

    The initial Delete, that get constraint error:
    Code:
    LOG:  execute S_1: BEGIN
    LOG:  execute <unnamed>: delete from t030_1_1_executores where t030_1_1_modelo_e=$1
    DETAIL:  parameters: $1 = '56'
    LOG:  execute <unnamed>: delete from t030_1_2_motivos where t030_1_2_modelo_e=$1
    DETAIL:  parameters: $1 = '56'
    LOG:  execute <unnamed>: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1
    DETAIL:  parameters: $1 = '56'
    ERROR:  update or delete on table "t030_1_conferencia_preco_modelo" violates foreign key constraint "t012_job_modelo_fk" on table "t012_job_cria_acao_conferencia_preco"
    DETAIL:  Key (t030_1_conferencia_preco_modelo_iu)=(56) is still referenced from table "t012_job_cria_acao_conferencia_preco".
    STATEMENT:  delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1
    ERROR:  current transaction is aborted, commands ignored until end of transaction block
    LOG:  execute S_2: ROLLBACK
    After that, I try to insert a new Entity of the same type. This is the generated statement:

    Code:
    LOG:  execute <unnamed>: select nextval ('t030_atividade_modelo_base_t030_atividade_modelo_base_iu_seq')
    LOG:  execute <unnamed>: select motivo0_.t020_motivo_nao_conferencia_iu as t1_9_0_, motivo0_.t020_descricao as t2_9_0_, motivo0_.t020_motivo as t3_9_0_ from t020_motivos_nao_conferencia motivo0_ where motivo0_.t020_motivo_nao_conferencia_iu=$1
    DETAIL:  parameters: $1 = '33'
    LOG:  execute <unnamed>: insert into t030_atividade_modelo_base (t030_data_criacao, t030_descricao, t030_nome, t030_usuario_criacao_e, t030_atividade_modelo_base_iu) values ($1, $2, $3, $4, $5)
    DETAIL:  parameters: $1 = '2011-04-07 11:23:26.443', $2 = NULL, $3 = 'teste160', $4 = '26', $5 = '71'
    LOG:  execute <unnamed>: insert into t030_1_conferencia_preco_modelo (t030_1_duracao_minutos, t030_1_conferencia_preco_modelo_iu) values ($1, $2)
    DETAIL:  parameters: $1 = '10', $2 = '71'
    LOG:  execute <unnamed>: insert into t030_1_1_executores (t030_1_1_modelo_e, t030_1_1_usuario_e) values ($1, $2)
    DETAIL:  parameters: $1 = '71', $2 = '24'
    LOG:  execute <unnamed>: insert into t030_1_2_motivos (t030_1_2_modelo_e, t030_1_2_motivo_e) values ($1, $2)
    DETAIL:  parameters: $1 = '71', $2 = '33'
    LOG:  execute <unnamed>: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1
    DETAIL:  parameters: $1 = '56'
    ERROR:  update or delete on table "t030_1_conferencia_preco_modelo" violates foreign key constraint "t030_2_motivos_modelo_fk" on table "t030_1_2_motivos"
    DETAIL:  Key (t030_1_conferencia_preco_modelo_iu)=(56) is still referenced from table "t030_1_2_motivos".
    STATEMENT:  delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1
    As you can see, Hibernate try to delete again the entity, I dont invoke any delete methods. I only try to insert a new entity.

    Someone could help? I cant found any post related this problem!

    Many thanks, and sorry by bad english.

  • #2
    Another strange thing. After this error, all persistence of this entity seems to be confused. I cant even delete the entity, because Hibernate does not follow some mapping like many to many relations, where will be necessary a cascade delete in order to satisfy data integrity.

    I have to restart the application, in order to use persistence correct in this entity.

    Only a observation. I use a Extended Persistence Context.

    Many thanks by any help.

    Comment

    Working...
    X