I managed to install the admin app and created a new DataSource
However, I'm still having the error:
org.hibernate.exception.GenericJDBCException: Cannot open connection
-Hibernate 3
-Tomcat 5.5.12
My DataSource that I set up with Tomcat's admin app looks like this:
Code:
name: jdbc/test
url: jdbc:mysql://localhost:3306/test?autoReconnect=true
driver: com.mysql.jdbc.Driver
username: test
password: ******
Note: It is possible to connect to the database from mysql's command line.
my hibernate.cfg.xml file looks like this:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/test</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="/BaseHelloWorldObject.hbm.xml" />
</session-factory>
</hibernate-configuration>
BaseHelloWorldObject.hbm.xml looks like this:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mypackage.HelloWorld.pojo.BaseHelloWorldObject" table="hello_world">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" type="string" column="name"/>
<property name="message" type="string" column="message"/>
<property name="messageDate" type="timestamp" column="message_date"/>
</class>
</hibernate-mapping>
HibernateUtil.java looks like this:
Code:
public class HibernateUtil
{
public static final SessionFactory sessionFactory;
static
{
try
{
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException
{
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null)
{
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException
{
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
}
And the error is thrown at:
Code:
Session session = HibernateUtil.currentSession();
System.out.println("got session");
System.out.println("getting transaction");
Transaction tx = session.beginTransaction(); <-- ERROR
The error is:
Code:
org.hibernate.exception.GenericJDBCException: Cannot open connection
org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:301)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:110)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:137)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:49)
org.hibernate.transaction.JDBCTransactionFactory.beginTransaction(JDBCTransactionFactory.java:24)
org.hibernate.jdbc.JDBCContext.beginTransaction(JDBCContext.java:271)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1079)
com.mypackage.HelloWorld.action.HelloWorldAction.test(HelloWorldAction.java:30)
[/code]