When can Hibernate SessionFactory be used?
pirent420 Sep 15, 2014 11:44 PMHi all,
I have been migrating from Jboss 5.1.0 GA to 6.2.0 EAP and struggling with the Hibernate SessionFactory. As in JBoss 5, to use a Hibernate SessionFactory, I look up it via its JNDI name which is specified in the service-hibernate.xml.
<hibernate-configuration xmlns="urn:jboss:hibernate-deployer:1.0"> <session-factory name="java:/hibernate/NvrMcSessionFactory" bean="nstc:service=NvrMcHibernate">
Here is my Singleton bean (in style of Jboss 5 and EJB 3.0) that needs the sessionFactory as long as it starts up:
@Service @Management(ServiceMBean.class) @Depends"nstc:service=NvrMcHibernate") @Local(BackupDbManager.class) public class BackupDbManagerBean extends ServiceMBeanSupport implements BackupDbManager { private final ListenerInitializationHelper hibListenerInitHelper = new ListenerInitializationHelper( "BackupDbListener", NvrMcHibernate.getSessionFactory());
The lookup code:
public synchronized SessionFactory lookup() { if (sessionFactory == null) { try { InitialContext ic = new InitialContext(); this.sessionFactory = (SessionFactory)ic.lookup(jndiName); } catch (NamingException e) { Assert.fail("Failed to lookup hibernate session factory: " + jndiName, e); } } return sessionFactory; }
As far as I know, in Jboss 5, Hibernate is configured in MBean and the org.hibernate.jmx.HibernateService take care of creating SessionFactory and binds it to the specified JNDI name.
Now JBoss EAP 6 no longer supports @org.jboss.annotation.ejb.Service and the service-hibernate.xml. So I have to switch to EJB 3.1 @Singleton and @Startup annotations. For the sessionFactory configuration, I replace the service-hibernate.xml with the hibernate.cfg.xml. I also change the JNDI name from "java:/hibernate/NvrMcSessionFactory" to "java:jboss/hibernate/NvrMcSessionFactory" for new JNDI naming.
<hibernate-configuration> <session-factory name="java:jboss/hibernate/NvrMcSessionFactory"> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.103.148:3306/mydb</property> <property name="hibernate.connection.username">username</property> <property name="hibernate.dialect">nstc.framework.hibernate.NecMySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.session_factory_name">java:jboss/hibernate/NvrMcSessionFactory</property> </session-factory> </hibernate-configuration>
When I deploy my application, the singleton bean cannot found the sessionFactory via the JNDI name. I am not sure if the sessionFactory is not constructed and binding before the singleton bean startup or it is not constructed yet. I have read the Migration Guide: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.2/html/Migration_Guide/index.html.
But it seems like the it does not cover this case. I have googled but I cannot find the answer (or I do not use the right keyword).
Did I missed something? Please help me know how can I use the sessionFactory in JBoss 6 EAP simply as in JBoss 5.