import java.sql.Connection;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.jdbc.Work;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TestIsolation {
@Test
public void testIsolationLevel() {
Connection cc = null;
Session session = null;
Transaction txn = null;
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
session = entityManager.unwrap(Session.class);
txn = session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
Connection c = connection;
c.toString();
System.out.println("Transaction isolation level is " + Environment.isolationLevelToString(connection.getTransactionIsolation() ) );
}
});
txn.commit();
} catch (RuntimeException e) {
if ( txn != null && txn.isActive() ) txn.rollback();
throw e;
} finally {
if (session != null) {
session.close();
}
}
}
}
|