11.2.4. Transaction timeout

An important feature provided by a managed environment like EJB is transaction timeout, which is never provided in non-managed code. 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

    // do some work

catch (RuntimeException e) {
    throw e; // or display error message
finally {
setTimeout() cannot be called in a CMT bean, where transaction timeouts must be defined declaratively.