Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
54
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

Transaction timeout

opinion, we should not force the application developer to catch an unrecoverable exception at a low layer. In most systems, unchecked and fatal exceptions are handled in one of the first frames of the method call stack (i.e., in higher layers) and either an error message is presented to the application user or some other appropriate action is taken. Note that Hibernate might also throw other unchecked exceptions that are not a HibernateException. These are not recoverable and appropriate action should be taken.

Hibernate wraps SQLExceptions thrown while interacting with the database in a

JDBCException. In fact, Hibernate will attempt to convert the exception into a more meaningful subclass of JDBCException. The underlying SQLException is always available via JDBCException.getCause(). Hibernate converts the SQLException into an appropriate

JDBCException subclass using the SQLExceptionConverter attached to the SessionFactory. By default, the SQLExceptionConverter is defined by the configured dialect. However, it is also possible to plug in a custom implementation. See the javadocs for the

SQLExceptionConverterFactory class for details. The standard JDBCException subtypes are:

JDBCConnectionException: indicates an error with the underlying JDBC communication.

SQLGrammarException: indicates a grammar or syntax problem with the issued SQL.

ConstraintViolationException: indicates some form of integrity constraint violation.

LockAcquisitionException: indicates an error acquiring a lock level necessary to perform the requested operation.

GenericJDBCException: a generic exception which did not fall into any of the other categories.

13.2.4. Transaction timeout

An important feature provided by a managed environment like EJB, that is never provided for non-managed code, is transaction timeout. Transaction timeouts ensure that no misbehaving transaction can indefinitely tie up resources while returning no response to the user. Outside a managed (JTA) environment, Hibernate cannot fully provide this functionality. However, Hibernate can at least control data access operations, ensuring that database level deadlocks and queries with huge result sets are limited by a defined timeout. In a managed environment, Hibernate can delegate transaction timeout to JTA. This functionality is abstracted by the Hibernate Transaction object.

Session sess = factory.openSession(); try {

//set transaction timeout to 3 seconds sess.getTransaction().setTimeout(3); sess.getTransaction().begin();

// do some work

...

sess.getTransaction().commit()

}

249

Соседние файлы в папке more