title: How to use a singleton Session factory in an application with multiple modules
body: <p>I have a session factory bean defined like this:</p>
  
  <pre><code>   &lt;bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
      &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSource" 
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
          &lt;property name="url" value="jdbc:h2:tcp://localhost/~/test" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>I know it's a bit weird that the configuration data is in another file when I can just write it directly here in the bean definition. But it does work.    </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
      &lt;hibernate-configuration&gt;
       &lt;session-factory&gt;
        &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/property&gt;
        &lt;property name="hibernate.connection.username"&gt;sa&lt;/property&gt;
        &lt;property name="hibernate.connection.password"&gt;&lt;/property&gt;
        &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
       &lt;/session-factory&gt;
      &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>So I'm trying to do some work with the session factory. And It's working. I'm connected to a database, I can use the session factory to save or update to or fetch things from the database. The implementation works. </p>
  
  <p>Now I'm trying to use the same Session factory in another module. I use spring to inject this mySessionFactory bean in it. And it doesn't work.</p>
  
  <p>Then I change the scope of the mySessionFactory bean to "prototype" and everything works well in both modules.</p>
  
  <p>Now I'm guessing that the problem is that when the SessionFactory is a singleton it can't be seen in different modules. Here is an example of a method which uses the SessionFactory:</p>
  
  <pre><code>public T fetchById(Class&lt;T&gt; p_type, Long p_id) {
  
          Session session;
          Transaction transaction;
  
          T returnT;
  
          session = sessionFactory.openSession();
  
          transaction = session.beginTransaction();
  
          returnT = (T) session.get(p_type, p_id);
  
          transaction.commit();
          session.close();
  
          return returnT;
  
      } 
  </code></pre>
  
  <p>I have a couple of these and, again, they all work in the entire application if the mySessionFactory is a prototype, so I don't think it's a dependency issue. But the methods only work in the module where the bean is defined if mySessionFactory is the default singleton.</p>
  
  <p>The Exception I get is:</p>
  
  <p>org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]</p>
  
  <p>My question is what approach to take to use a single instance of the SessionFactory in the entire app. I don't want to create a new SessionFactory every time a method is called.</p>
  
  <p>I don't have a thorough understanding of threads in java so I'm not sure what's the problem. I can only kinda guess. I'm sorry if this question is lacking information.</p>
  
  <p>Thanks for the help.</p>