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 tomcat>conf:
<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?
I was also facing the same issue but found some link .Please have a look at it. In 2 day I will try to do the hands-on and will let you know the result. https://forum.hibernate.org/viewtopic.php?f=1&t=1003866&start=15
in thelink he had mentioned one point
As JNDI of tomcat is read-only, only Tomcat itself has the right to bind object to JNDI. For Hibernate it is therefore not enough to declare the datasource as Resource in Context.xml. You must also declare the UserTransaction as Resource in Context.xml as described in the linked article, because Hibernate with JTA expects the UserTransaction to be already bound on JNDI. and also the link
Please try it out .