How to use a Datasource configured in Tomcat 5 with hibernate?
I have configured the datasource in server.xml as usual.
Then I configured the hibernate.cfg.xml file to use that Datasource so:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/hibernate</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I use this code in my servlet to test connections:
public class Init extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
SessionFactory sessionFactory;
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session s = sessionFactory.openSession();
s.connection();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
}
but when it execute openSession() the application throws an exception saying that there are problem with connection. The log of the problem is here:
java.lang.ExceptionInInitializerError
at servlets.Init.init(Init.java:26)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1019)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3991)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4335)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
at org.apache.catalina.core.StandardHostDeployer.addChild(StandardHostDeployer.java:903)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:252)
at org.apache.commons.digester.SetNextRule.end(SetNextRule.java:256)
at org.apache.commons.digester.Rule.end(Rule.java:276)
at org.apache.commons.digester.Digester.endElement(Digester.java:1058)
at org.apache.catalina.util.CatalinaDigester.endElement(CatalinaDigester.java:76)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.commons.digester.Digester.parse(Digester.java:1567)
at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:488)
at org.apache.catalina.core.StandardHost.install(StandardHost.java:863)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:482)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:427)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:964)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
at org.apache.catalina.core.StandardService.start(StandardService.java:476)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2298)
at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:284)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:422)
Caused by: java.lang.UnsupportedOperationException
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:116)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:554)
at net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:56)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:286)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3326)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3286)
at servlets.Init.init(Init.java:23)
... 46 more
2004-10-12 17:02:39 StandardContext[/hibernate]17:02:36,438 INFO Environment:469 - Hibernate 2.1.6
17:02:36,448 INFO Environment:503 - loaded properties from resource hibernate.properties: {hibernate.connection.username=agori, hibernate.connection.password=sagra, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.connection.url=jdbc:mysql:///hibernate, hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N'}
17:02:36,458 INFO Environment:529 - using CGLIB reflection optimizer
17:02:36,468 INFO Configuration:895 - configuring from resource: /hibernate.cfg.xml
17:02:36,468 INFO Configuration:867 - Configuration resource: /hibernate.cfg.xml
17:02:36,578 INFO Configuration:331 - Mapping resource: User.hbm.xml
17:02:36,708 INFO Binder:229 - Mapping class: beans.User -> USER
17:02:36,828 INFO Configuration:1053 - Configured SessionFactory: null
17:02:36,828 INFO Configuration:627 - processing one-to-many association mappings
17:02:36,828 INFO Configuration:636 - processing one-to-one association property references
17:02:36,828 INFO Configuration:661 - processing foreign key constraints
17:02:36,858 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
17:02:36,868 INFO SettingsFactory:59 - Maximim outer join fetch depth: 2
17:02:36,868 INFO SettingsFactory:63 - Use outer join fetching: true
17:02:36,878 INFO NamingHelper:26 - JNDI InitialContext properties:{}
17:02:36,898 INFO DatasourceConnectionProvider:51 - Using datasource: java:comp/env/jdbc/hibernate
17:02:36,908 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
17:02:37,109 INFO SettingsFactory:103 - Use scrollable result sets: false
17:02:37,109 INFO SettingsFactory:106 - Use JDBC3 getGeneratedKeys(): false
17:02:37,109 INFO SettingsFactory:109 - Optimize cache for minimal puts: false
17:02:37,189 INFO SettingsFactory:118 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
17:02:37,189 INFO SettingsFactory:129 - cache provider: net.sf.hibernate.cache.EhCacheProvider
17:02:37,209 INFO Configuration:1116 - instantiating and configuring caches
17:02:37,930 INFO SessionFactoryImpl:118 - building session factory
17:02:38,681 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
..
What is wrong??
Thanks
|