无法在Spring应用程序中处理@Transactional方法中的hibernate异常

前端之家收集整理的这篇文章主要介绍了无法在Spring应用程序中处理@Transactional方法中的hibernate异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有个问题.我使用Spring Hibernate,我无法处理标记为@Transactional的方法中的异常.以前,当我使用Spring JDBC时,一切工作都很好.

DAO类标有@Repository.
这是我旧代码的秘密.
在服务中:

@Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,rollbackFor = {Exception.class})
        public boolean bookTickets(Integer userId,List

 
在道:
 

@Transactional(propagation = Propagation.MANDATORY)
public void bookTicket(Ticket ticket,final int userId) {
    MapsqlParameterSource map = new MapsqlParameterSource();
    map.addValue(Constants.TABLE_BOOKING.FIELD_TICKET_ID,ticket.getId());
    map.addValue(Constants.TABLE_BOOKING.FIELD_USER_ID,userId);

    int rowsAffected = template.update(QUERY_INSERT_BOOKING,map);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Affected " + rowsAffected + " rows.");
    }
}

现在我将我的DAO从Spring JDBC移到hibernate 3上.
这就是我现在拥有的.
 
在服务中:

@Transactional(readOnly = false,rollbackFor = {Exception.class})
public boolean bookTickets(Integer userId,List

在道:
 

@Override
public void bookTicket(Booking booking) {
    getHibernateTemplate().save(booking);
}

问题是我无法在服务方法中处理try-catch块中的异常.他们直接投入我的控制器.
 

我认为问题在于Spring JDBC中的事务管理器org.springframework.jdbc.datasource.DataSourceTransactionManager和hibernate中的org.springframework.orm.hibernate3.HibernateTransactionManager,但是我没有任何想法如何克服这个问题,所以我需要你的帮助.
  
  
更新后.增加的例外情况.

2013-10-21 14:51:17 ERROR BookingController:89 - Unhandled exception
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; sql [insert into booking (ticket_id,user_id,booking_id) values (?,?,?)]; constraint ["CONSTRAINT_INDEX_2 ON PUBLIC.BOOKING(TICKET_ID) VALUES ( /* key:1 */ 2,1,1)"; sql statement:
insert into booking (ticket_id,?) [23505-173]]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
    at cdp.tarasenko.springmvc.task3.service.TicketsService$$EnhancerByCGLIB$$fabdc899.bookTickets(sqlStateConverter.convert(sqlStateConverter.java:71)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
    ... 51 more
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Нарушение уникального индекса или первичного ключа: "CONSTRAINT_INDEX_2 ON PUBLIC.BOOKING(TICKET_ID) VALUES ( /* key:1 */ 2,1)"
Unique index or primary key violation: "CONSTRAINT_INDEX_2 ON PUBLIC.BOOKING(TICKET_ID) VALUES ( /* key:1 */ 2,?) [23505-173]
    at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1167)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
    ... 59 more

有没有办法处理服务层内的异常?

谢谢.

最佳答案
看一下hibernate FlushMode

默认HiberanateTemplate FlushMode是FlusMode.AUTO,因此在提交时或在某些查询之前会发生会话同步以防止过时状态.

您可以将HibernateTemplate刷新模式更改为FlushMode.ALWAYS,效率低且不推荐(但会按预期工作),或者在某些时候调用Session.flush().

请参阅HiberanateTemplate.setFlushMode()和Session.flush()

原文链接:https://www.f2er.com/spring/431787.html

猜你在找的Spring相关文章