Propagation en Full annotation
Bonjour à tous,
Débutant en spring et hibernate je profite de ce sujet pour vous faire part d'un problème lié aux transactions (en espérant ne pas me tromper de post ..)
J'utilise Spring 2.5.5 ,hibernate 3.2.1.ga , mysql et Tomcat
//interface de mon service
Code:
1 2 3
| public interface IFaxService {
public IFaxModel saveFaxModel(IFaxModel faxModel);
} |
//implémentation de mon service
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
@Service("faxServiceTarget")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public class FaxService implements IFaxService {
private IFaxDao faxDao;
@Autowired
public void setFaxDao(@Qualifier("faxDao") IFaxDao faxDao) {
this.faxDao = faxDao;
}
public IFaxDao getFaxDao() {
return faxDao;
}
public IFaxModel saveFaxModel(IFaxModel faxModel) {
return getFaxDao().saveFaxModel(faxModel);
}
} |
l'objet qui utilise mon service
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class TestFaxFactory{
private IFaxService faxService;
@Autowired
public void setFaxService(@Qualifier("faxService") IFaxService faxService) {
this.faxService = faxService;
}
public IFaxService getFaxService() {
return faxService;
}
public void testSaveFaxMod(){
IFaxModel faxModel_1 = (IFaxModel) getXmlBeanFactory().getBean("faxModel_1");
faxModel_1 = getFaxService().saveFaxModel(faxModel_1);
}
} |
mon fichier de conf 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
|
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tool" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="fax"/>
<bean id="faxDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="fax.dao.IFaxDao"/>
<property name="interceptorNames">
<list>
<value>faxDaoInterceptor</value>
<value>faxDaoBean</value>
</list>
</property>
</bean>
<bean id="faxService" parent="transactionProxy">
<property name="target" ref="faxServiceTarget"/>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="transactionProxy" 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="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
scope="singleton">
<property name="configLocation" value="classpath:test.hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.c3p0.autoCommitOnClose">true</prop>
<prop key="hibernate.autoCommitOnClose">true</prop>
<prop key="hibernate.autocommit">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.transaction.auto_close_session">true</prop>
</props>
</property>
</bean>
</beans> |
avec cette configuration le faxModel que je sauve est effectivement bien sauvé en base (je peux le voire via MySql browser ... )
Je voudrais avoir le moins possible de xml dans mon fichier spring. (d'ou les annotation dans mes classes)
Mes questions sont donc les suivantes :
Y a t il un moyen (annotations particulières , méthodes ou classes à implémenter ... ) pour ne pas avoir à définir les morceaux de codes suivant de mon fichier de conf spring :
Code:
1 2 3 4 5 6 7
|
<bean id="faxService" parent="transactionProxy">
<property name="target" ref="faxServiceTarget"/>
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>
</property>
</bean> |
et même dans l'absolue si il y a un moyen de ne pas avoir a définir dans le fichier de conf de spring la description du transactionProxy et du transactionManager
Code:
1 2 3 4 5 6 7 8 9 10
|
<bean id="transactionProxy" 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> |
Code:
1 2 3 4 5 6
|
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean> |
j'ai bien essayé d'injecter directement mon service "faxServiceTarget" dans mon objet de test de la manière suivante
Code:
1 2 3 4 5 6 7 8 9
|
public class TestFaxFactory {
[...]
@Autowired
public void setFaxService(@Qualifier("faxServiceTarget") IFaxService faxService) {
this.faxService = faxService;
[..]
} |
mais en faisant de la sorte, je n'ai plus de transaction et bien que je n'ai aucune erreur, mon faxModel n'est pas sauvegardé en base...
Une âme charitable aurait elle à défaut d'une solution , une piste à me proposer ?
MErci d'avance !