How to configure a single Hibernate SessionFactory for multiple web applications

I have n number of web applications talking to a single database using hibernate. I need to configure JBOSS in such a way that there will be single Hibernate SessionFactory and the web applications will use this SessionFactory to obtain Sessions. Which is the best optimal way to acheive this solution?
visu_nekkAsked:
Who is Participating?
 
ramazanyichCommented:
jboss (starting from jboss 4.0.2) provides special mbean which will provide hibernate session factory.
you could check it in jboss documentation http://docs.jboss.org/jbossas/jboss4guide/r4/html/ch13.html.
Basically you need to create xml file in deploy folder of jboss:
something like:
<mbean code="org.jboss.hibernate.jmx.Hibernate"
       name="jboss.adminguide:name=ExampleSessionFactory">
    <attribute name="DatasourceName">java:/DefaultDS</attribute>
    <attribute name="Dialect">
        org.hibernate.dialect.HSQLDialect
    </attribute>
    <attribute name="SessionFactoryName">
        java:/hibernate/ExampleSessionFactory
    </attribute>
</mbean>
Just point it to correct database ds jndi URL (I used defaultDS which will use HSQLDb)
and after that you can lookup sessionfactory using JNDI lookups from your code:

InitialContext ctx      = new InitialContext();
SessionFactory factory  = (SessionFactory)
                          ctx.lookup("java:/hibernate/ExampleSessionFactory");
Session        hsession = factory.openSession();
0
 
ramazanyichCommented:
everything depends in implementation of your web application. How do you call sessionfactory inside your web app? Do you use Spring? do you use EJB3 ?
for Spring there is a some discussion which is available at http://forum.springsource.org/archive/index.php/t-43521.html
0
 
visu_nekkAuthor Commented:
We are using a combination of Struts with Hibernate.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.