-->
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: Is this the recommended way?
PostPosted: Tue Jul 27, 2004 2:21 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Hi,

I had been working to setup Hibernate 2.1.4 with Weblogic 8.1 and I have some doubts.

Hibernate In Action Section 2.4.2 says :
Code:
The SessionFactory will automatically bind itself to JNDI if the property hibernate.session_factory_name is set to the name of the directory node.
efender also seem to suggest something like that, but I could not make it work automatically.

I had a glance at the code and got the feel that the JNDI registration happens in SessionFactoryObjectFactory.addInstance,which is called from the constructor of SessionFactoryImpl. This constructor gets called from Configuration.buildSessionFactory method.

Hence I ended up doing the following :
Code:
public class HibernatePOCEJB implements SessionBean
{
    public static final String WLS_SESSION_FACTORY = "hibernate/SessionFactory";
    private static Logger log = Logger.getLogger(HibernatePOCEJB.class);

    public void fetchLoan(String loanId) throws RemoteException
    {
        Session session = openSession();
        try
        {
            Loan loan = (Loan) session.load(Loan.class, Long.valueOf(loanId));
        }
        catch (HibernateException e)
        {
            throw new RemoteException("", e);
        }
        catch (NumberFormatException e)
        {
            throw new RemoteException("", e);
        }
        finally
        {
            try
            {
                if (session != null)
                {
                    session.close();
                }
            }
            catch (HibernateException e)
            {
                throw new RuntimeException("", e);
            }
        }
    }

    private Session openSession() throws RemoteException
    {
        SessionFactory sessions = null;
        try
        {
            log.debug("Looking up SessionFactory in JNDI");
            Context ctx = new InitialContext();
            sessions = (SessionFactory) ctx.lookup(WLS_SESSION_FACTORY);
            log.debug("Looked up SessionFactory in JNDI");
        }
        catch (NameNotFoundException e)
        {
            try
            {
                sessions = new Configuration().configure().buildSessionFactory();
                log.debug("Could not find a SessionFactory in JNDI, hence created one");
            }
            catch (HibernateException e1)
            {
                throw new RemoteException("", e1);
            }
        }
        catch (NamingException e)
        {
            throw new RemoteException("", e);
        }
        try {
            log.debug("Opening new Hibernate Session");
            return sessions.openSession();
        }
        catch (HibernateException e)
        {
            throw new RemoteException("", e);
        }
    }
}

Code:
<hibernate-configuration>
    <session-factory name="hibernate.session-factory.Oracle">
        <property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
        <property name="jndi.url">t3://localhost:7001</property>
    <!-- .. -->
       
    </session-factory>

</hibernate-configuration>


This seems to work for me, although I havent tested this throughly. Kindly let me know if this is the recommended way (if there is one :) to use Hibernate with Weblogic.

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject: OOPS!
PostPosted: Tue Jul 27, 2004 2:24 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
I was under the impression that I was posting a reply to this thread, but ended up posting a new topic by accident.

My appologies.

_________________
Thanks,
Binil Thomas


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.