Java Reference
In-Depth Information
Figure 8-2.
The anatomy of a deadlock
Looking at the database after completion, you will see that the
test
user has been
replaced with either
jeff
or
dave
in both tables (you will never see
dave
from one thread and
jeff
from the other). Though it is not necessary here, because we close the session regardless,
in a more extensive application it is important to ensure that the session associated with a
deadlock or any other Hibernate exception is closed and never used again because the cache
may be left in a corrupted state.
It is worth building and running Listing 8-3 to ensure that you are familiar with the
symptoms of a deadlock when they occur.
Listing 8-3.
Code to Generate a Deadlock
package com.hibernatebook.session.deadlock;
import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class GenerateDeadlock {
private static SessionFactory factory = new Configuration().configure()
.buildSessionFactory();
public static void createUser(String username) throws HibernateException {
Session session = factory.openSession();
try {
session.beginTransaction();
Search WWH ::
Custom Search