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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java App Servers

From novice to tech pro — start learning today.