Results 1 to 4 of 4

Thread: SLSB, DAO and hibernateSessionFactory management

  1. #1
    Join Date
    Dec 2004
    Location
    London
    Posts
    51

    Default SLSB, DAO and hibernateSessionFactory management

    Hi

    Can someone please help me out here: We have SLSB CMT and our SLSBs are deferring their business logic to a pojo (using the spring abstract ejb convenience class).

    Our hibernate session factory has so far been setup using a jboss sar file and deployed as such making the hibernate session factory available via a JNDI call.

    After implementing the business pojo, I am trying to get the daoImpl (also injected)'s baseclass, 'HibernateBaseDAO', to hold onto the hibernate session factory and return a session for each request. But the factory is null when I try to use it in my daoImpl. (I have defined an init-method in the bean definition so that the factory can be set via a jndi-call. I have also tried using the JndiObjectFactoryBean):

    </snippet>
    <bean id="hibernateFactory" class="org.springframework.jndi.JndiObjectFactoryB ean" singleton="true">
    <property name="jndiName" value="java:/cbs/hibernate/HibernateFactory"/>
    </bean>
    <bean id="hibernateSessionFactoryInit" class="uk.co.mycom.cbs.dao.HibernateDAO" init-method="init" singleton="false">
    <property name="factory" ref="hibernateFactory"/>
    </bean>
    </snippet>

    HibernateBaseDAO :
    <snippet>
    private static SessionFactory factory;
    protected static final String HIBERNATE_FACTORY = "java:/cbs/hibernate/HibernateFactory";

    public void init(){
    log.debug("init: begin!");
    System.out.println("init: begin!");
    try{
    InitialContext initialContext = new InitialContext();
    factory = (SessionFactory) initialContext.lookup(HIBERNATE_FACTORY);
    System.out.println("init: factory = :" + factory + ":!");
    initialContext.close();
    }
    catch (Exception e)
    {
    log.error("Exception getting datasource: "+e);
    }
    System.out.println("init: end!");
    log.debug("init: end!");
    }

    /**
    * @param factory The factory to set.
    */
    public static void setFactory(SessionFactory factory) {
    HibernateDAO.factory = factory;
    }
    </snippet>

    Am I missing something obvious here? The factory appears in the jndi-tree as shown in the jmx-console. Also is there a perhaps a better way to do this?

    Any help is much appreciated.
    Jim

  2. #2
    Join Date
    Nov 2005
    Location
    Austria
    Posts
    38

    Default

    Why is the 'factory' property declared static? Does the setup for JndiObjectFactoryBean really work? I'm missing the setting of the 'proxyInterface' property?
    Perhaps a better way is to use the ContextSingletonBeanFactoryLocator (see Spring reference documentation).

  3. #3
    Join Date
    Jul 2005
    Posts
    2

    Default Same error...

    I'm seeing the same error. If anyone finds a fix for this problem please post!

    with hibernate 3.0.1, spring 1.2.1, and jboss 4.0.2:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is javax.naming.NamingException: JNDI object with [java:/hibernate/AuctionSessionFactory] not found: JNDI implementation returned null
    javax.naming.NamingException: JNDI object with [java:/hibernate/AuctionSessionFactory] not found: JNDI implementation returned null
    at org.springframework.jndi.JndiTemplate$1.doInContex t(JndiTemplate.java:125)
    at org.springframework.jndi.JndiTemplate.execute(Jndi Template.java:85)
    at org.springframework.jndi.JndiTemplate.lookup(JndiT emplate.java:121)
    at org.springframework.jndi.JndiLocatorSupport.lookup (JndiLocatorSupport.java:71)
    at org.springframework.jndi.JndiObjectLocator.lookup( JndiObjectLocator.java:85)
    at org.springframework.jndi.JndiObjectFactoryBean.aft erPropertiesSet(JndiObjectFactoryBean.java:124)

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    I would try to replace the static property and direct lookup with dependency injection.
    You can use org.springframework.jndi.JndiObjectFactoryBean for that purpose. When setting the property lookupOnStartup of this factory bean to false, access should be deferred until the first usage. This is normally sufficent to keep things working.

    Regards,
    Andreas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •