-->
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.  [ 6 posts ] 
Author Message
 Post subject: Could not locate SessionFactory in JNDI
PostPosted: Wed Aug 16, 2006 8:43 am 
Beginner
Beginner

Joined: Wed Aug 09, 2006 11:26 am
Posts: 20
Hello everyone,

I have generated daos for my appplication but there is a problem when I call the dao from a test class. The error I get is:

Could not locate SessionFactory in JNDI
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial


My config file:

<hibernate-configuration>
<session-factory name="java:/hibernate/HibernateFactory">
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<property name="connection.pool_size">2</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>

<!-- mappings -->

</session-factory>
</hibernate-configuration>

The getSessionFactory method within dao:

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("java:/hibernate/HibernateFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}



If I need to set values for hibernate.jndi.url and hibernate.jndi.class, what values should they be set to?
I use hibernate 3.1, oracle 10g and HibernateTools-3.2.0.beta6, no application server.

My test class initialises the session factory in a static block:

private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.info("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void main(String[] args){
session = getSessionFactory().openSession();
tx = session.beginTransaction();
TestDao dao = new TestDao();
......
}


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 8:59 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Since you are using Hibernate from a standalong environment you are really not binding the SessionFactory to the JNDI name you have specified.

Typically you would do this in a managed environment like inside a J2EE envrionment.

I'm not sure if your "static" block raises a Exception as if a name is specificed Hibernate would try to bind the SessionFactory to JNDI and in a standalong case you don't have a Naming Service that you can use, unless you probably use the dfault Naming service (File based if I'm correct) from Sun.

You may want to add the default jndi.properties that uses the Sun's default naming implementation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 9:50 am 
Beginner
Beginner

Joined: Wed Aug 09, 2006 11:26 am
Posts: 20
Thanks for your reply

I tried using the default values (found in the hibernate.properties file which comes with hibernate)

#hibernate.jndi.class com.sun.jndi.fscontext.RefFSContextFactory
#hibernate.jndi.url file:/

but I still get the same error. I read somewhere in the reference that if these values are not defined then the default InitialContext will be used. However the error persists if I don't define them at all.

It is possible to bind the session factory to jndi in standalone applications, is it not?
If I can't do it using jndi then I should I try passing it to daos through the constructor or just define a setter method?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 18, 2006 7:53 am 
Beginner
Beginner

Joined: Wed Aug 09, 2006 11:26 am
Posts: 20
Well I managed to solve this.

In the config file:
<property name="jndi.class">com.sun.jndi.fscontext.RefFSContextFactory</property>
<property name="jndi.url">file:/</property>

2 jars in my classpath:
fscontext.jar
providerutil.jar

In my dao:
protected SessionFactory getSessionFactory() {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
return (SessionFactory) new InitialContext(env).lookup("java:/hibernate/HibernateFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 19, 2007 8:32 pm 
Newbie

Joined: Sat Mar 17, 2007 2:17 pm
Posts: 6
EDIT: Solved! See below, in the next message.

I have the same problem but with your steps i'm unable to solve. My settings:

hibernate.cfg.xml
Code:
<session-factory>
        <property name="jndi.class">com.sun.jndi.fscontext.RefFSContextFactory</property>
        <property name="jndi.url">file:///tmp</property>
        <property name="hibernate.session_factory_name">hibernate/HibernateFactory</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost/model</property>
      <property name="connection.username">root</property>
      <property name="connection.password">root</property>
      <property name="connection.pool_size">2</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
      <property name="current_session_context_class">thread</property>
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <property name="show_sql">false</property>
      <mapping resource="config.hbm.xml"/>
   </session-factory>


A session initializated somewhere with:
Code:
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();


A similar dao class (here is the problem!!!!):
Code:
    protected SessionFactory getSessionFactory() {
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
            return (SessionFactory) new InitialContext(env).lookup("hibernate/HibernateFactory");
        }
        catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI");
        }
    }


The jndi context seems created (/tmp/hibernate/.bindings)
Code:
#This file is used by the JNDI FSContext.
#Tue Mar 20 01:23:07 CET 2007
HibernateFactory/RefAddr/0/Content=ff808181116cb9ca01116cb9cd760000
HibernateFactory/FactoryName=org.hibernate.impl.SessionFactoryObjectFactory
HibernateFactory/ClassName=org.hibernate.impl.SessionFactoryImpl
HibernateFactory/RefAddr/0/Encoding=String
HibernateFactory/RefAddr/0/Type=uuid


But Still i get this errors:

Code:
     [java] 01:23:07,947  WARN SessionFactoryObjectFactory:101 - InitialContext did not implement EventContext
     [java] 01:23:07,963 ERROR ProvinciaHome:33 - Could not locate SessionFactory in JNDI
     [java] javax.naming.NameNotFoundException: HibernateFactory
     [java]     at com.sun.jndi.fscontext.RefFSContext.getObjectFromBindings(RefFSContext.java:419)
     [java]     at com.sun.jndi.fscontext.RefFSContext.lookupObject(RefFSContext.java:347)
     [java]     at com.sun.jndi.fscontext.RefFSContext.lookup(RefFSContext.java:183)
     [java]     at com.sun.jndi.fscontext.FSContext.lookup(FSContext.java:165)
     [java]     at javax.naming.InitialContext.lookup(InitialContext.java:347)
     [java]     at com.thgroup.visionehrcv.model.ProvinciaHome.getSessionFactory(ProvinciaHome.java:30)
     [java]     at com.thgroup.visionehrcv.model.ProvinciaHome.<init>(ProvinciaHome.java:24)
     [java]     at com.thgroup.visionehrcv.model.util.VisioneHRCVUtil.popolaDB(VisioneHRCVUtil.java:63)
     [java]     at com.thgroup.visionehrcv.model.util.VisioneHRCVUtil.main(VisioneHRCVUtil.java:40)
     [java] Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
     [java]     at com.thgroup.visionehrcv.model.ProvinciaHome.getSessionFactory(ProvinciaHome.java:34)
     [java]     at com.thgroup.visionehrcv.model.ProvinciaHome.<init>(ProvinciaHome.java:24)
     [java]     at com.thgroup.visionehrcv.model.util.VisioneHRCVUtil.popolaDB(VisioneHRCVUtil.java:63)
     [java]     at com.thgroup.visionehrcv.model.util.VisioneHRCVUtil.main(VisioneHRCVUtil.java:40)
     [java] Java Result: 1



Please, help me. I can't go forward until i i get this to work :-(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 20, 2007 5:48 am 
Newbie

Joined: Sat Mar 17, 2007 2:17 pm
Posts: 6
The problem was a lacking initialization parameter. Unfortunately, a LOT of guide/tutorial on internet simply assume that you can write in your root directory,
setting things like <property name="jndi.url">file:/</property>, that probably belongs to C: or your %systemdrive% . Obviously in *nix, you can't write to / without being root, so you naturally set things like <property name="jndi.url">file:///tmp</property> as i did.
The problem is that in this case you have to specify hibernate to look at /tmp, instead of the default (probably / for *nix or C: for windows). Adding the line indicated resolve the problem, and seems the correct way to do it. Yes, this was probably a fscontext/jndi RTFM, but this should definitively be in the hibernate faqs: a lot of similar questions in the forum are still unanswered.

Code:
    protected SessionFactory getSessionFactory() {
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.fscontext.RefFSContextFactory");
            env.put (Context.PROVIDER_URL,"file:///tmp"); // <-- This solved my problem!
            return (SessionFactory) new InitialContext(env).lookup("java:hibernate/HibernateFactory");
        }
        catch (Exception e) {
            log.error("Could not locate SessionFactory in JNDI", e);
            throw new IllegalStateException("Could not locate SessionFactory in JNDI");
        }
    }


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