salut
j'utilise spring 1.2 et hibernate3

j'ai des dao pour les opertion CRUD
et des services pour la partie métier

la dao est gérée par spring et les transactions sont programmatique (pour l'instant) du style sessio.beginTransaction, sessio.rollback()...
ca marche bien

ensuite mes services utilise des dao: par exemple pour creer un compte client, il faut d'abord faire compteDAO.create() puis clientDAO.create() à la suite.
Le pb c que si ya une erreur dans le dernier create, il ya bien un rollback au niveau de la dao, mais pas pour toute les opérations du sevice (dans le cas présent, on peut avoir un compte de crer et pas le client)

Donc pour les services j'utilise les transaction déclaratives grace a spring AOP et le transactionManager de Hibernate: voici le bean.xml:


<beans>

<!-- ############# Source de donnée ############# -->
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/siteCom"/>
</bean>


<!-- ############# Hibernate SessionFactory ############# -->
<bean id="hibSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name="dataSource">
<ref local="myDataSource"/>
</property>
<property name="configLocation">
<value>WEB-INF/hibernate.cfg.xml</value>
</property>
</bean>



<!-- ############# Transaction Management for DAO ############# -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="hibSessionFactory"/>
</property>
</bean>


<bean id="userBL" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager"/>
<property name="target" ref="userBLTarget"/>
<property name="transactionAttributes">
<props>
<prop key="créerCompteClient*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>



<!-- ############# DAO ############# -->
<bean id="clientManager" class="domain.hibernate.ClientDAOHib">
<property name="sessionFactory">
<ref local="hibSessionFactory"/>
</property>
</bean>
<bean id="produitManager" class="domain.hibernate.ProduitDAOHib">
<property name="sessionFactory">
<ref local="hibSessionFactory"/>
</property>
</bean>
<bean id="commandeManager" class="domain.hibernate.CommandeDAOHib">
<property name="sessionFactory">
<ref local="hibSessionFactory"/>
</property>
</bean>
<bean id="compteManager" class="domain.hibernate.CompteDAOHib">
<property name="sessionFactory">
<ref local="hibSessionFactory"/>
</property>
</bean>




<!-- ############# Logique Métier ############# -->
<bean id="userBLTarget" class="logic.UserBLImpl">
<property name="clientManager" ref="clientManager" />
<property name="compteManager" ref="compteManager" />
</bean>


</beans>

... et voici le code du service:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public Client créerCompteClient(String nom, String prenom, 
                                    String rue, String ville, String codePostal,
                                    String numTel, String mail,
                                    String login, String pass ){
 
 
        Compte cpt = null;
        Client client = null;
        try{
            cpt = new Compte(login, pass, mail, new Role(Role.CLIENT)); // TODO passer en Role.nouvel-inscrit puis activer... & Role.CLIENT
            System.out.println("Création d'un Compte...!");
            // PB TRANSACTIONS: si la création du client échoue, le compte n'est pas supprimé?
            cpt = compteManager.createCompte(cpt);
 
            client = new Client(nom, prenom, numTel, rue, ville, codePostal, cpt);
            client = clientManager.createClient(client);
 
        }
        catch(Exception e){
            System.out.println("Erreru UserBL dans  Création compte client a echoué\n" + e.getMessage());
        }
 
 
 
        return client;
    }

Pourtant ca ne marche pas, je ne sais pas comment faire un rollback:
est ce qu'il faut lever une exception specifique? ne pas capturer d'exception?? est ce que PROPAGATION_REQUIRED est approprié? je ne comprend pas la signification des autres type de transaction!

g besoin d'un coup de main! mercvi d'avance