-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to use a Datasource configured in Tomcat 5?
PostPosted: Tue Oct 12, 2004 11:10 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
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


Top
 Profile  
 
 Post subject: The problem is in the DataSource
PostPosted: Tue Oct 12, 2004 12:57 pm 
Newbie

Joined: Fri Oct 08, 2004 11:59 am
Posts: 16
Location: Helsinki, Finland
The problem lies in this part of the stack trace:

Code:
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)


Something in class PoolingDataSource throws an UnsupportedOperationException.
So this probably has nothing to do with Hibernate. There seems to be something wrong with the DataSource instead.
I don't have the source code of Jakarta's commons-dbcp, but you
should find the answer to your problem from there.
And check the configuration of your DataSource.

- Ville Peurala


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 3:10 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
Ok the problem was that i have to remove hibernate.properties from the classpath!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 22, 2004 10:03 am 
Newbie

Joined: Fri Oct 22, 2004 9:57 am
Posts: 1
agori wrote:
Ok the problem was that i have to remove hibernate.properties from the classpath!


I have the exact same problem, what did you mean by removing the hibernate.properties from the classpath?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 7:38 am 
Beginner
Beginner

Joined: Fri Jan 13, 2006 8:07 am
Posts: 29
Hi,

Have you got the problem ( UnSupportedOperationException ) solved ?

If then, can you please tell me how did you solve that problem ? I have got the same problem.

Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 26, 2006 7:20 pm 
Newbie

Joined: Wed Jul 27, 2005 11:20 am
Posts: 19
If you're using Tomcat 5.5.15, you can use the following procedures:

1) Add the following lines to your conf/server.xml:
<Server>
...
<GlobalNamingResources>
<Resource name="jdbc/rsr" auth="Container" type="javax.sql.DataSource"
username="root" password="xxxx" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://webserv:3306/rsr" maxActive="0" maxIdle="100"
maxWait="10000" removedAbandoned="true" logAbandoned="true" />

...
</GlobalNamingResources>

2) Add the following bold lines to your conf/context.xml:

<!-- The contents of this file will be loaded for each web application -->
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->

<!-- Don Ngo: Provide JDBC DataSource for all applications -->
<ResourceLink name="jdbc/rsr" type="javax.sql.DataSource"
global="jdbc/rsr" />

</Context>

3) Add the following lines to your hibernate.properties files and make sure it is located in the application classpath (I would recommend you put in the Tomcat's shared/classes folder so that it will accessible by all web apps):

# JNDI Data Source
hibernate.connection.datasource = java:/comp/env/jdbc/rsr

# MySQL Configuration
hibernate.dialect = org.hibernate.dialect.MySQLDialect

_________________
Don Ngo


Top
 Profile  
 
 Post subject: Thank You
PostPosted: Tue Sep 12, 2006 5:19 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Just a quick "Thanks!" to dngo. You're step-by-step on how to use the Tomcat connection pool as a JNDI resource worked like a charm.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.