I want to define a DataSource to an Oracle database on my Tomcat 6.0.
So, in conf/server.xml
(yes, I know that this DataSource will be available for all the webapps in Tomcat, but it's not a problem here), I've set this Resource:
<GlobalNamingResources>
<Resource name="hibernate/HibernateDS" auth="Container" type="javax.sql.DataSource"
url="jdbc:oracle:thin:@myserver:1542:foo"
username="foo" password="bar"
driverClassName="oracle.jdbc.OracleDriver"
maxActive="50" maxIdle="10" validationQuery="select 1 from dual"/>
Then, in the web.xml
of my application, I set a resource-ref
element:
<resource-ref>
<description>Hibernate Datasource</description>
<res-ref-name>hibernate/HibernateDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Finally, as Hibernate is used to manage the database connection, I have a webapps/mywebapp/WEB-INF/classes/hibernate.cfg.xml
that creates a session-factory
using the JNDI DataSource:
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/hibernate/HibernateDS</property>
...
However, when I start my Tomcat server, I get an error that says it could not create the
INFO [net.sf.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
INFO [net.sf.hibernate.connection.DatasourceConnectionProvider] Using datasource: java:comp/env/hibernate/HibernateDS
INFO [net.sf.hibernate.transaction.TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
INFO [net.sf.hibernate.transaction.TransactionManagerLookupFactory] No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
WARN [net.sf.hibernate.cfg.SettingsFactory] Could not obtain connection metadata
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:59)
at net.sf.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
at net.sf.hibernate.cfg.Configuration.buildSettings(Configuration.java:1172)
...
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)
at java.sql.DriverManager.getDriver(DriverManager.java:253)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
... 11 more
Do you have any idea why Hibernate is not able to construct the session-factory
? What is wrong in my configuration?
Did you place the Oracle JDBC Jar in Tomcat's
common/lib
folder? Since Tomcat does not support Oracle DB connections out of the box, it may simply be missing the proper library.Your resource definition and application configuration look fine to me.
your resource definition is missing attribute driverClassName