Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!

iarizona iarizona

+ Follow
since Nov 27, 2002
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by iarizona iarizona

Even, as a SCJA and SCJP I had to try 3 times to get Hibernate to work under Tomcat 5.0.19.

The example in the Hibernate web site is horrible, which is a shame for this emerging technology.

So I created a very simple example that works under Tomcat 5 and mySQL.

Hopefull this will help others get started.

The JSP page is named index.jsp:

<%@ page import="*" %>
<%@ page import="java.util.*" %>
<%@ page import="mypackage.*" %>
<%@ page import="org.hibernate.*" %>
<%@ page import="org.hibernate.cfg.*" %>
org.hibernate.Session hibernateSession = mypackage.HibernateUtil.currentSession();
Transaction tx = hibernateSession.beginTransaction();

Person person = new Person();
Query query = hibernateSession.createQuery("select p from Person as p where p.myName=:name");
query.setString("name", "Joe");
for (Iterator iter = query.iterate(); iter.hasNext() {
person = (Person);
<table width="400" border="0" cellspacing="1" cellpadding="0" align="center" class="tableBox">
<td CLASS="bluebanner" align="center"> Greetings, <%=person.getMyName()%></TD>

The web.xml is:

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"




<!-- Servlets -->

<!-- Servlet Mappings -->

<!-- Session Expires in 1 day -->

<!-- The Welcome File List -->

<!-- Data Source References -->

<description>DB Connection</description>


The Java source is in directory /WEB-INF/src:

package mypackage;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.out.println("Initial SessionFactory creation failed: " + ex.getMessage());
throw new ExceptionInInitializerError(ex);

public static final ThreadLocal hibernateSession = new ThreadLocal();

public static Session currentSession() {
Session s = (Session) hibernateSession.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
return s;

public static void closeSession() {
Session s = (Session) hibernateSession.get();
if (s != null)

package mypackage;

public class Person {

private String myName;
private String id;

public String getId() {
return id;

private void setId(String id) { = id;

public String getMyName() {
return myName;

public void setMyName(String name) {
this.myName = name;

The hibernate.cfg.xml configuration files is:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"



<property name="connection.datasource">java:comp/env/jdbc/hibernate</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Mapping files -->
<mapping resource="Person.hbm.xml"/>



And the Person.hbm.xml file is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"


<class name="mypackage.Person" table="Person">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>

<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="myName">
<column name="name" length="80" not-null="true"/>



The context xml for this example in Tomcat's server.xml is:

<Context path="/Hibernate" reloadable="true" docBase="C:\eclipse\workspace\Hibernate" workDir="C:\eclipse\workspace\hibernate\work">
<Resource name="jdbc/hibernate" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/hibernate">


Good luck!
When a worker thread is hung in a synchronous IO operation (such as a JDBC call) here is a trick I developed on how to kill the worker thread as well as the supervisory thread.
You may say "but this is brute force, not very elegant!"
Well, at least this brute solution works!
Try using interrupt() or stop() (deprecated, by the way). The worker thread does not respond at all...
So this is the only solution I found.

* Start thread

public void run () {
cat.debug(Thread.currentThread().getName() + " thread started.");

if (Thread.currentThread().getName().equalsIgnoreCase("SUPERVISOR")) {
try {
} catch (InterruptedException e) {}

//Supervisor wokeup

if (queryInProgress) {
stopProcessing = true;
cat.debug(worker.thread.getName() + " will be aborted by supervisory monitor. Query exceeded threshold.");
else {
//Worker thread
I just got my SCJA certification and here is my advice:
Study hard!
You need to have in-depth knowledge of EJB's, Servlets, Patterns, UML and connectivity to legacy systems.
Do not take this exam (all 3 parts) lightly because it is intended for more seasoned developers with solid experience in Java and Object-Oriented principles.
In the end you will be proud of your accomplishment and you will be rewarded by having a very prestigious certification.
Good luck to all of you.