休眠异常不会被捕获

时间:2019-04-08 21:28:28

标签: java exception

我面临的问题是,每个try catch块都被忽略了。

我有这样的结构:

void A(){
   // some logic
   try{
       service.createItem(...);
   }catch(Exception e){
       System.out.println("blabla...");
   }
}

service$createItem(...){
   // some logic - building item.
   try{
      dao.saveItem( item );
   }catch(Exception e){
      throw new IllegalArgumentException("bla bla");
   }
}

dao$saveItem(Item item){
        Session session = sessionFactory.getCurrentSession();
        session.save(item);
}

项是唯一的,即使我尝试插入重复项,也不会捕获异常并打印“ bla bla”,而是仅打印整个堆栈跟踪信息-就像根本没有处理异常一样。

调试显示它甚至没有跳到A方法的catch块中,我以前没有遇到过类似的事情,有人遇到过这种情况吗?

什么可能导致问题?

谢谢!

stackStrace:

  

用于:org.hibernate.exception.ConstraintViolationException:可以   在不执行语句   org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3167)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3682)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3283)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2479)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access $ 300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:609)   〜[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE] ...另外35个原因:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   键“ PRIMARY”的条目“ 2-1”重复,位于   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)   〜[?:1.8.0_121]在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)   〜[?:1.8.0_121]在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)   〜[?:1.8.0_121]在   java.lang.reflect.Constructor.newInstance(Constructor.java:423)   〜[?:1.8.0_121]在   com.mysql.jdbc.Util.handleNewInstance(Util.java:425)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.Util.getInstance(Util.java:408)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)   〜[mysql-connector-java-5.1.46.jar:5.1.46]在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3167)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3682)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:90)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3283)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2479)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:178)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access $ 300(JdbcResourceLocalTransactionCoordinatorImpl.java:39)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl $ TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:271)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)   〜[hibernate-core-5.3.2.Final.jar:5.3.2.Final]在   org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:609)   〜[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE] ...还有35个

     

原因:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:   键“ PRIMARY”的条目“ 2-1”重复

1 个答案:

答案 0 :(得分:2)

您无法以这种方式捕获它的原因是,在事务刷新期间而不是在try-catch或save块期间出现异常。

也许考虑在再次保存之前检查数据库中是否存在该记录?

因此,要解决此问题,必须对事务型的类/方法进行尝试捕获,或将事务型移至保存到数据库的类。