我有个问题.我使用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
有没有办法处理服务层内的异常?
谢谢.
最佳答案