Announcement Announcement Module
Collapse
No announcement yet.
Rebuilding the SessionFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rebuilding the SessionFactory

    Hi,

    I have already read thread http://forum.springframework.org/sho...sessionFactory and have tried to follow the advice there but I'm still having a problem.

    I have an application where the user can define new tables in the database through our gui and then start to populate them. Our gui adds additional mappings into a single hbm.xml file for this purpose. At the moment the user needs to bounce the server after creating the table so that the revised mapping file is reloaded into the sessionFactory correctly.

    I have tried to rebuild the session factory at runtime as follows:

    LocalSessionFactoryBean lfsb = (LocalSessionFactoryBean)BeanFactory.getBean("&ses sionFactory");
    sessionFactory = lfsb.getConfiguration().buildSessionFactory();
    hibernateTemplate = new HibernateTemplate(sessionFactory);

    Our DAO's call the above code to get a new hibernate template whenever the mapping files have been amended but this fails when building the session factory with the following message:

    org.hibernate.HibernateException: No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean
    at org.springframework.orm.hibernate3.LocalDataSource ConnectionProvider.configure(LocalDataSourceConnec tionProvider.java:48)
    at org.hibernate.connection.ConnectionProviderFactory .newConnectionProvider(ConnectionProviderFactory.j ava:80)
    at org.hibernate.cfg.SettingsFactory.createConnection Provider(SettingsFactory.java:362)
    at org.hibernate.cfg.SettingsFactory.buildSettings(Se ttingsFactory.java:60)
    at org.hibernate.cfg.Configuration.buildSettings(Conf iguration.java:1463)
    at org.hibernate.cfg.Configuration.buildSessionFactor y(Configuration.java:1004)
    at com.accipia.centerview.dao.StreamItemSessionFactor yFactoryImpl.renewSessionFactory(StreamItemSession FactoryFactoryImpl.java:35)
    at com.accipia.centerview.dao.StreamItemSessionFactor yFactoryImpl.getHibernateTemplate(StreamItemSessio nFactoryFactoryImpl.java:27)
    at com.accipia.centerview.dao.StreamItemDAOHibernate. getHibernateTemplate(StreamItemDAOHibernate.java:8 0)
    at com.accipia.centerview.dao.StreamItemDAOHibernate. findAllBySet(StreamItemDAOHibernate.java:35)
    at com.accipia.centerview.analyser.framework.Calculat ionManagerImpl.populateInputStreams(CalculationMan agerImpl.java:257)
    at com.accipia.centerview.analyser.framework.Calculat ionManagerImpl.setUpFunctionTaskContext(Calculatio nManagerImpl.java:195)
    at com.accipia.centerview.analyser.framework.Calculat ionManagerImpl.executeTask(CalculationManagerImpl. java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:335)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:181)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :148)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:96)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :170)
    at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:176)
    at $Proxy1.executeTask(Unknown Source)
    at tests.integration.analysisEngine.TestMerge.testMer geMultipleOutputRecords(TestMerge.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:154 )
    at junit.framework.TestCase.runBare(TestCase.java:127 )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at junit.framework.TestResult.run(TestResult.java:109 )
    at junit.framework.TestCase.run(TestCase.java:118)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit 3TestReference.run(JUnit3TestReference.java:128)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:196)

    My spring config files are set up as follows:

    <bean id="hibernateProperties" class="org.springframework.beans.factory.config.Pr opertiesFactoryBean">
    <property name="properties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Orac le9Dialect</prop>
    <prop key="hibernate.order_updates">true</prop>
    <prop key="hibernate.jdbc.batch_size">1000</prop>
    <prop key="hibernate.c3p0.minPoolSize">5</prop>
    <prop key="hibernate.c3p0.maxPoolSize">20</prop>
    <prop key="hibernate.c3p0.timeout">600</prop>
    <prop key="hibernate.c3p0.max_statement">50</prop>
    </props>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <ref bean="hibernateProperties" />
    </property>
    <property name="mappingLocations">
    <value>classpath*:**/*.hbm.xml</value>
    </property>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="url">
    <value>jdbc:oracle:thin:@localhost:1521:david1</value>
    </property>
    <property name="username">
    <value>foo</value>
    </property>
    <property name="password">
    <value>bar</value>
    </property>
    </bean>


    This configuration runs fine most of the time and only fails when I try to recreate the sessionFactory after chaning the mapping file.

    Any help would be greatly appreciated.

    As an alternative I tried creating a new Configuration from scratch using the following code:

    Configuration cfg = new Configuration()
    .addDirectory(dir)
    .addResource(Constants.CUSTOMER_MAPPING_DIR + "/" + Constants.CUSTOMER_MAPPING_FILE)
    .setProperties(hibernateProperties)
    .setProperty("hibernate.connection.driver_class",d ataSource.getDriverClassName())
    .setProperty("hibernate.connection.url",dataSource .getUrl())
    .setProperty("hibernate.connection.username",dataS ource.getUsername())
    .setProperty("hibernate.connection.password",dataS ource.getPassword());

    sessionFactory = cfg.buildSessionFactory();
    hibernateTemplate = new HibernateTemplate(sessionFactory);

    where datasource and hibernateProperties are both injected in from the spring configs shown above.

    Although this seems to work when I run individual tests, if I am running a suite of tests then I get the following message:

    ORA-00054:, resource busy and acquire with NOWAIT specified.

    I don't get this message if I replace the above code to simply get the original spring configured sessionFactory and live with bouncing the server after changing the mapping file.

  • #2
    If you look at the lifecycle methods of the LocalSystemFactoryBean, I think they would be your best bet. They setup and teardown the factoryBean, exactly what you want to do.

    Code:
    	/**
    	 * Build and expose the SessionFactory.
    	 * @see #buildSessionFactory()
    	 * @see #wrapSessionFactoryIfNecessary
    	 */
    	public void afterPropertiesSet() throws Exception {
    		SessionFactory rawSf = buildSessionFactory();
    		this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf);
    		afterSessionFactoryCreation();
    	}

    Comment

    Working...
    X