JNDI Datasource Problem on Tomcat 6, Hibernate
- by Asuman AKYILDIZ
I am using Tomcat 6 as application server, Struts-Hibernate and MyEclipse 6.0.
My application uses JDBC driver but I should modify it to use JNDI Datasource. I followed steps as described in tomcat 6.0 howto tutorial.
I defined my resource in tomcatconf:
<Resource name="jdbc/ats" global="jdbc/ats" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@//localhost:1521/MISDEV"
username="TEST" password="TEST" maxActive="20" maxIdle="10"
maxWait="-1" validationQuery="SELECT 1 from dual"
removeAbandoned="true"
removeAbandonedTimeout="30"
logAbandoned="false"/>
I gave reference in my application web.xml:
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/ats</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And I defined datasource-dialect in my hibernate-cfg.xml
<property name="connection.datasource">java:comp/env/jdbc/ats</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
But when I create hibernate session, it can not open the connection:
09:18:11,322 ERROR JDBCExceptionReporter:72 - Connections could not be acquired from the underlying database!
org.hibernate.exception.GenericJDBCException: Cannot open connection
I also tried to set the properties at runtime:
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
//configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/ats");
configuration.setProperty("hibernate.current_session_context_class", "thread");
configuration.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
configuration.setProperty("hibernate.show_sql", "true");
sessionFactory = configuration.configure().buildSessionFactory();
It does not open connection again.
But, when I use JDBC driver it works:
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
//configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/ats");
configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@//localhost:1521/MISDEV");
configuration.setProperty("hibernate.connection.username", "test");
configuration.setProperty("hibernate.connection.password", "test");
configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver");
configuration.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
configuration.setProperty("hibernate.current_session_context_class", "thread");
configuration.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
configuration.setProperty("hibernate.show_sql", "true");
sessionFactory = configuration.configure().buildSessionFactory();
I have been searching for 3 days and no success. What may be de problem?