gestion declarative des transactions avec spring 2.5.5 et hibernate
Bonjour a tous. je tente de configurer la gestion des transaction declarative avec spring 2.5.5 en vain.
Il me met l'erreur suivante qui signifie qu'aucune transaction n'est lancee/
Code:
1 2 3 4
|
...
saveOrUpdate is not valid without active transaction
... |
Dans mes recherche j'ai constate que hibernate.current_session_context_class dans le fichier de conf hibernate pouvait creer problème. quant j'enleve ce tag
il me dit qu'aucune session n'est bindee au threa courant.
Code:
1 2
|
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here |
Mon fichier de config spring
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- ******************************************************************************************* -->
<!-- DEFINITION D'UN GESTIONNAIRE DE TRANSACTION POUR HIBERNATE -->
<!-- ****************************************************************************************** -->
<!-- sessionFactory -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Gestionnaire de transaction Hibernate -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<!-- ******************************************************************************************* -->
<!-- DEFINITION DES DAO -->
<!-- ****************************************************************************************** -->
<!-- Ensemble des différents DAO -->
<bean id="daoAccount" class="org.os.easyguard.daopojo.DAOAccount">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<bean id="daoApplication"
class="org.os.easyguard.daopojo.DAOApplication">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<bean id="daoGroup" class="org.os.easyguard.daopojo.DAOGroup">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<bean id="daoResource"
class="org.os.easyguard.daopojo.DAOResource">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<bean id="daoRole" class="org.os.easyguard.daopojo.DAORole">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<bean id="daoUser" class="org.os.easyguard.daopojo.DAOUser">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice"
transaction-manager="txManager">
<!-- the transactional semantics... -->
<tx:attributes>
<!-- all methods starting with 'get' are read-only -->
<tx:method name="*"
propagation="REQUIRED"
rollback-for="oued.ressources.ServiceLayerException" />
</tx:attributes>
</tx:advice>
<!-- ******************************************************************************************* -->
<!-- GESTION TRANSACTIONNELLE POUR LE SERVICE org.os.easyguard.service.application.Application -->
<!-- ****************************************************************************************** -->
<!-- ensure that the above transactional advice runs for any execution
of an operation defined by the FooService interface -->
<aop:config>
<aop:pointcut id="applicationOperation" expression="execution(* org.os.easyguard.service.application.IApplication.*(...))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="applicationOperation"/>
</aop:config>
<bean id="application"
class="org.os.easyguard.service.application.ApplicationImpl">
<property name="daoApplication">
<ref bean="daoApplication" />
</property>
<property name="daoResource">
<ref bean="daoResource" />
</property>
</bean>
</beans> |
Hibernate.cfg.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://10.3.1.8/easyGuardDB</property>
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<property name="hibernate.c3p0.min_size">20</property>
<property name="c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">20</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.current_session_context_class">
thread
</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="org\os\easyguard\daopojo\Role.hbm.xml"/>
<mapping resource="org\os\easyguard\daopojo\Application.hbm.xml"/>
<mapping resource="org\os\easyguard\daopojo\User.hbm.xml"/>
<mapping resource="org\os\easyguard\daopojo\Group.hbm.xml"/>
<mapping resource="org\os\easyguard\daopojo\Account.hbm.xml"/>
<mapping resource="org\os\easyguard\daopojo\Resource.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Invocation de mon service
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
ClassPathResource res = new ClassPathResource("service-config.xml"); // La fabrique SPRING est charg�e, les singletons sont cr��s XmlBeanFactory factory = new XmlBeanFactory(res); // On utilise la m�thode getBean en passant le nom du bean pour cr�er // ou r�cup�rer un bean d�clar� dans le fichier de configuration IVehiculeServices vservices = (IVehiculeServices)factory.getbean("myVehiculeServices");
// La fabrique SPRING est charg�e, les singletons sont cr��s
XmlBeanFactory factory = new XmlBeanFactory(res);
application=(IApplication)factory.getBean("application");
....
try {
application.createApplication("peace", resList);
} catch (ServiceLayerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} |