-->
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.  [ 2 posts ] 
Author Message
 Post subject: Getting DataSource from tomcat
PostPosted: Tue Nov 16, 2010 7:42 pm 
Newbie

Joined: Wed Oct 20, 2010 11:22 am
Posts: 14
Sorry, pretty new to hibernate/tomcat.

I would like to use tomcat's connection pooling for my hibernate project. I first got the project working with a direct connect to the db, but when I try to switch to tomcat connection pooling and use a data source (yes, I deployed it to tomcat), I keep getting a "datasource not found". Can somebody please let me know what I'm doing wrong? I have looked at the forums and googled for help for the entire day! Just can't get it to work!

Thanks for any help!!!

Tomcat v5.5
hibernate v3.6

First I added a resource to the tomcat/conf/context.xml file:
Code:
<Resource name="app1/DB" auth="Container"
        type="javax.sql.DataSource"
        driverClassName="com.informix.jdbc.IfxDriver"
        username="usr1" password="usr1"
        url="jdbc:informix-sqli://us:9999/mydb:INFORMIXSERVER=testtcp1;dbdate=MDY4"
        validationQuery="select first 1 type from animals"
        defaultAutoCommit="false"
        maxActive="10" maxIdle="5" maxWait="1000"
        removeAbandoned="true" removeAbandonedTimeout="300" logAbandoned="true"/>



I then configured the persistence.xml file as follows:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="HibernateTest" transaction-type="RESOURCE_LOCAL">

      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      
      <class>jpa.common.Class1</class>
   
      <properties>
         <property name="hibernate.connection.datasource" value="app1/DB" />
      </properties>

   </persistence-unit>
</persistence>


And finally, the file I use to get the data:
Code:
public class MyService
{
   public static EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateTest");
   public static EntityManager em;
   
   public List<Class1> getClass1(int type)
   {
      em =  emf.createEntityManager();
      
      em.getTransaction().begin();

      String classQuery="select c from Class1 c where c.type = :type";
      Query q = em.createQuery(classQuery);
      q.setParameter("type", type);
      
      @SuppressWarnings("unchecked")
      List<Class1> classs = q.getResultList();
   }


The Error I'm getting is:
Code:
610 [TP-Processor2] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
610 [TP-Processor2] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: app1/DB
javax.naming.NameNotFoundException: Name app1is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
    at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
    at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:51)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2833)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2829)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)


Top
 Profile  
 
 Post subject: Re: Getting DataSource from tomcat
PostPosted: Wed Nov 17, 2010 1:07 pm 
Newbie

Joined: Wed Oct 20, 2010 11:22 am
Posts: 14
OK,
I finally found it on another forum.

This is my new persistence.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="HibernateTest" transaction-type="RESOURCE_LOCAL">

      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <non-jta-data-source>java:comp/env/fpcservlets/DB</non-jta-data-source>
      
      <class>jpa.common.Class1</class>

   </persistence-unit>
</persistence>


Not sure why, but you must use the "<non-jta-data-source>" element and you must include the "java:comp/env" in front of the DataSource name that was entered as a resource in the tomcat/conf/context.xml file.

Note sure why you can't use the "<property .../>" element.

Hope this helps someone!!!!

The link to the other forum's message is:
http://forums.oracle.com/forums/thread.jspa?messageID=1899677


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.