Right, the context have a close method but, when i manually call it i get an exception at hibernate schema export step:
i have written a very simple unit test case that fails:
Code:
package coach.service.impl.basic;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class HibernateSchemaExport extends TestCase {
ClassPathXmlApplicationContext apc;
protected void setUp() throws Exception {
super.setUp();
apc = new ClassPathXmlApplicationContext("coach/applicationContext.xml");
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testApcClose() {
apc.close();
}
}
the stack trace clearly points out that HB tries to drop the schema (when it calls <Running hbm2ddl schema export>)
Code:
2005-12-19 19:18:33,054 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Retrieving dependent beans for bean 'userDao'>
2005-12-19 19:18:33,054 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Invoking destroy() on bean with name 'sessionFactory'>
2005-12-19 19:18:33,054 INFO [org.springframework.orm.hibernate3.LocalSessionFactoryBean] - <Closing Hibernate SessionFactory>
2005-12-19 19:18:33,054 INFO [org.hibernate.impl.SessionFactoryImpl] - <closing>
2005-12-19 19:18:33,054 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] - <Running hbm2ddl schema export>
2005-12-19 19:18:33,054 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] - <exporting generated schema to database>
2005-12-19 19:18:33,054 INFO [org.hibernate.connection.ConnectionProviderFactory] - <Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider>
2005-12-19 19:18:33,054 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] - <schema export unsuccessful>
org.hibernate.HibernateException: No local DataSource found for configuration - dataSource property must be set on LocalSessionFactoryBean
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.configure(LocalDataSourceConnectionProvider.java:48)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:80)
at org.hibernate.tool.hbm2ddl.SchemaExport$ProviderConnectionHelper.getConnection(SchemaExport.java:431)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:130)
at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:108)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:812)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean$TransactionAwareInvocationHandler.invoke(LocalSessionFactoryBean.java:1021)
at $Proxy0.close(Unknown Source)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:982)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.destroy(AbstractBeanFactory.java:923)
at org.springframework.beans.factory.support.AbstractBeanFactory.destroyBean(AbstractBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractBeanFactory.destroyDisposableBean(AbstractBeanFactory.java:986)
at org.springframework.beans.factory.support.AbstractBeanFactory.destroySingletons(AbstractBeanFactory.java:557)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:530)
at coach.service.impl.basic.HibernateSchemaExport.testApcClose(HibernateSchemaExport.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java: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 junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
My config file wires the dataSource bean to the LocalSessionFactory, but Spring closes it too much soon, don't you think ??
here is my config file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- Permet de maintenir les parametres hibernate dans un fichier properties -->
<bean id="placeholderConfig1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/hibernate.properties</value>
</property>
</bean>
<!-- DataSource defintion -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
<!-- Session factory pour Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<value>classpath*:coach/model/impl/basic</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">
${hibernate.show_sql}
</prop>
<prop key="hibernate.max_fetch_depth">
${hibernate.max_fetch_depth}
</prop>
<prop key="hibernate.hbm2ddl.auto">
${hibernate.hbm2ddl.auto}
</prop>
<prop key="hibernate.cache.provider_class">
${hibernate.cache.provider_class}
</prop>
</props>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<!-- Gestionnaire de transaction pour Hibernate -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- DAO pour la gestion des Unites -->
<bean id="uniteDao" class="coach.dao.impl.hibernate.UniteDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- DAO pour la gestion des Configuration -->
<bean id="configurationDao"
class="coach.dao.impl.hibernate.ConfigurationDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- DAO pour la gestion des user -->
<bean id="userDao" class="coach.dao.impl.hibernate.UserDao">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Service de creation des objets du model -->
<bean id="coachModelFactory"
class="coach.model.impl.basic.ModelBasicFactory">
</bean>
<!-- Service de gestion des taches -->
<bean id="gestionnaireTacheTarget"
class="coach.service.impl.basic.GestionnaireTache">
</bean>
<!-- Service de gestion des ressources -->
<bean id="gestionnaireRessourceTarget"
class="coach.service.impl.basic.GestionnaireRessource">
</bean>
<!-- Service de gestion des configurations -->
<bean id="gestionnaireConfigurationTarget"
class="coach.service.impl.basic.GestionnaireConfiguration">
<property name="configurationDao">
<ref local="configurationDao" />
</property>
</bean>
<!-- Service d'init -->
<bean id="initServiceTarget"
class="coach.service.impl.basic.InitService">
<property name="availableServices">
<map>
<entry>
<key>
<value>gestionnaireTacheTarget</value>
</key>
<ref bean="gestionnaireTacheTarget" />
</entry>
<entry>
<key>
<value>gestionnaireRessourceTarget</value>
</key>
<ref bean="gestionnaireRessourceTarget" />
</entry>
<entry>
<key>
<value>gestionnaireConfigurationTarget</value>
</key>
<ref bean="gestionnaireConfigurationTarget" />
</entry>
</map>
</property>
<property name="coachModelFactory">
<ref bean="coachModelFactory" />
</property>
</bean>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="gestionnaireConfigurationProxy"
parent="txProxyTemplate">
<property name="target">
<ref bean="gestionnaireConfigurationTarget" />
</property>
</bean>
</beans>