11.2.4. Transaction timeout

One extremely 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 functioanlity 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 {
Note that setTimeout() may not be called in a CMT bean, where transaction timeouts must be defined declaratively.