0

次の方法でデータを保存しながら例外チェックを行っています。

    try {
        session.beginTransaction();
        session.update(person);
        session.getTransaction().commit();
    }
    catch(Exception e) {
        session.getTransaction().rollback();
        log.error("Error saving person to database", e);
    }

それにもかかわらず、後でどこかで例外を除いてアプリケーションを終了させました。

(データ切り捨てエラーを回避するためにこのチェックを行いました。Hibernateで例外を発生させずにデータ切り捨てをチェックする方法はありますか?)

4

1 に答える 1

1

When a Hibernate exception is thrown, you cannot use the session, which caused the exception, any more. A rollback is done automatically.

If you want to continue after the exception, you have two possibilities:

  1. Throw away the old session and create a new one.

  2. Use a StatelessSession instead of a session. A StatelessSession can be used after an exception. In a StatelessSession you have to do the rollback manually.

Normally you'll do solution 1.

Solution 2. is useful if you intentionally provoke an exception and you have a special way to react on it. (For example an index violation exception in an insert operation, and after the exception you do an update instead of an insert.)

于 2012-09-25T15:18:51.093 に答える