IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Java Discussion :

rollback sur transaction ne semble pas fonctionner [Data]


Sujet :

Spring Java

  1. #1
    Membre expérimenté Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Points : 1 639
    Points
    1 639
    Par défaut rollback sur transaction ne semble pas fonctionner
    Je travaille avec Spring2 et Hibernate3.2, j'essaye de gerer les transaction au niveau service, mais ceci ne semble pas fonctionner.
    En tout cas, quand une exception est levée, aucun rollback n'est executé.

    Voici mon fichier de configuration :

    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
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     
     
    	<!-- BEANS DAOS -->
    	<bean id="livreDao" class="fr.tigf.sbt.dao.hibernate.LivreDao" >
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    	</bean>
     
    	<!-- BEANS SERVICES -->
    	<bean id="biblioService" class="fr.tigf.sbt.service.impl.BiblioService">
    		<property name="livreDao" ref="livreDao" />
    	</bean>
     
     
     	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    		<property name="url" value="jdbc:oracle:thin:@10.33.31.147:1521:AAA" />
    		<property name="username" value="XXX" />
    		<property name="password" value="XXXX" />
    	</bean>
    	<bean id="sessionFactory" 
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    			</props>
    		</property>
    		<property name="mappingResources">
    			<list>
    					<value>mapping/Livre.hbm.xml</value>
    			</list>
    		</property>		
    	</bean>
     
    	<!-- Hibernate Template Defintion -->
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    		<property name="jdbcExceptionTranslator">
    			<ref bean="jdbcExceptionTranslator" />
    		</property>
    	</bean>
     
    	<!-- Spring Data Access Exception Translator Definition -->
    	<bean id="jdbcExceptionTranslator"
    		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
     
     
    	<!--  Gestionnaire de transaction. -->
    	<bean id="serviceManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
     
    	<!-- Service avec support de transaction -->
    	<bean name="interceptorService"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="serviceManager" />
    		</property>
     
    		<property name="target">
    			<ref local="biblioService" />
    		</property>
     
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED,
    				-fr.tigf.sbt.service.exception.NomExistantException
    				</prop>
    			</props>
    		</property>
    	</bean>
     </beans>
    Je provoque l'exception NomExistantException qui est bien levée, mais les opérations précédentes ont été comitées quand même.

    Il semblrait donc que mon service n'est pas interceptépar mon proxy.

    Si vous avez des idées, je suis preneur.

  2. #2
    Rédacteur
    Avatar de Hikage
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    1 177
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : Belgique

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 177
    Points : 6 301
    Points
    6 301
    Par défaut
    tu utilise comment ton "service"?

    Parce que ta config ton "interceptorService" est un proxy, et c'est donc lui tu dois injecter à la place de "biblioService".

    A ta place, pour être clair, je renommerai "biblioService" en "biblioServiceTarget" ( c'est le service cible ).

    et je renommerai 'interceptorService" en "biblioService", car c'est ce service qui dois être utilise pour que les transactions soient gérées par Spring.
    Hikage
    SCJP / SCWCD & SCWSJD Certified / Spring Framework Certified
    [Personal Web] [CV]

    F.A.Q Spring Framework - Participez !

  3. #3
    Membre expérimenté Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Points : 1 639
    Points
    1 639
    Par défaut
    J'ai fais comme tu m'as dit en changeant les noms mais ca ne change rien.
    Pas de rollback

    Ca donne ca :

    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
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     
     
    	<!-- BEANS DAOS -->
    	<bean id="livreDao" class="fr.tigf.sbt.dao.hibernate.LivreDao" >
    		<property name="hibernateTemplate">
    			<ref bean="hibernateTemplate" />
    		</property>
    	</bean>
     
    	<!-- BEANS SERVICES -->
    	<bean id="biblioServiceTarget" class="fr.tigf.sbt.service.impl.BiblioService">
    		<property name="livreDao" ref="livreDao" />
    	</bean>
     
     
     	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    		<property name="url" value="jdbc:oracle:thin:@10.33.31.147:1521:AAA" />
    		<property name="username" value="XXX" />
    		<property name="password" value="XXXX" />
    	</bean>
    	<bean id="sessionFactory" 
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    			</props>
    		</property>
    		<property name="mappingResources">
    			<list>
    					<value>mapping/Livre.hbm.xml</value>
    			</list>
    		</property>		
    	</bean>
     
    	<!-- Hibernate Template Defintion -->
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    		<property name="jdbcExceptionTranslator">
    			<ref bean="jdbcExceptionTranslator" />
    		</property>
    	</bean>
     
    	<!-- Spring Data Access Exception Translator Definition -->
    	<bean id="jdbcExceptionTranslator"
    		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
     
     
    	<!--  Gestionnaire de transaction. -->
    	<bean id="serviceManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
     
    	<!-- Service avec support de transaction -->
    	<bean name="biblioService"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref local="serviceManager" />
    		</property>
     
    		<property name="target">
    			<ref local="biblioServiceTarget" />
    		</property>
     
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_REQUIRED,
    				-fr.tigf.sbt.service.exception.NomExistantException
    				</prop>
    			</props>
    		</property>
    	</bean>
     </beans>

    Mon service est chargé comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    static IBiblioService biblioService;
    static String[] configFiles = new String[]{ "config.xml","persistence.xml" };
    	static ApplicationContext applicationContext;
    	private static void initialiseSpringContext(){
    		applicationContext = new ClassPathXmlApplicationContext(configFiles , true);
    		biblioService=(IBiblioService) applicationContext.getBean("biblioService");
    }
    je l'utilise ainsi (je tente de creer 2 livres avec le meme id):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    biblioService.ajouterLivre(new Livre("1984",new Date(),458,21.15F,"Ce livre parle de choses"));
    			biblioService.ajouterLivre(new Livre("La guerre des mondes",new Date(),327,0F,"autre livre"));
    			biblioService.ajouterLivre(new Livre("1984",new Date(),458,21.15F,"Ce livre parle de choses"));
    Et voici le code de mon 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
    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
    package fr.tigf.sbt.service.impl;
     
    import java.util.Collection;
     
    import fr.tigf.sbt.dao.ILivreDao;
    import fr.tigf.sbt.dao.exception.DAOException;
    import fr.tigf.sbt.service.IBiblioService;
    import fr.tigf.sbt.service.exception.LivreInexistantException;
    import fr.tigf.sbt.service.exception.NomExistantException;
    import fr.tigf.sbt.vo.ILivre;
     
    public class BiblioService implements IBiblioService{
    	ILivreDao livreDao;
     
     
    	public ILivre ajouterLivre(ILivre livre) throws NomExistantException,DAOException {
    		ILivre livreCree;
     
    		try {
    			lireLivre(livre.getNom());
    			throw new NomExistantException();
    		} catch (LivreInexistantException e) {
    			livreCree = livreDao.enregistrer(livre);
    		}
    		return livreCree;
    	}
     
    	public void enleverLivre(ILivre livre) throws LivreInexistantException,DAOException {
    		livreDao.supprimer(livre);
    	}
     
    	public ILivre lireLivre(String nom) throws LivreInexistantException,DAOException {
    		ILivre livre=livreDao.lire(nom);
    		if (livre==null)
    			throw new LivreInexistantException(nom);
    		return livre;
    	}
     
    	public Collection<ILivre> listerLivres() throws DAOException {
    		return livreDao.lister();
    	}
     
    	public ILivre modifierLivre(ILivre livre) throws LivreInexistantException,DAOException {
    		return livreDao.modifier(livre);
    	}
     
     
    	public void ajouterLivres(Collection<ILivre> listeLivres)
    			throws NomExistantException, DAOException {
    		for (ILivre livre:listeLivres){
    			ajouterLivre(livre);
    		}
    	}
     
    	public ILivreDao getLivreDao() {
    		return livreDao;
    	}
     
    	public void setLivreDao(ILivreDao livreDao) {
    		this.livreDao = livreDao;
    	}
     
    }

  4. #4
    Membre expérimenté Avatar de willoi
    Profil pro
    Développeur informatique
    Inscrit en
    Décembre 2006
    Messages
    1 355
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Décembre 2006
    Messages : 1 355
    Points : 1 639
    Points
    1 639
    Par défaut
    Bon finalement voici comment je l'ai resolu :

    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
    <bean id="transactionProxy" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref bean="transactionManager"/>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="insert*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="*">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
     
    	<bean id="transactionAttributeSource"
    		class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource"/>

    Et au niveau service j'utilise les anotations :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    @Transactional  (readOnly=false, propagation=Propagation.REQUIRED,rollbackFor={ MetierException.class })

    Mais la question que je me pose est de savoir comment proceder si je n'etais pas en Java1.5

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. setFont ne semble pas fonctionner
    Par Nico57 dans le forum AWT/Swing
    Réponses: 8
    Dernier message: 25/07/2007, 14h37
  2. [DOM] Script qui ne semble pas fonctionner sous IE 6 et 7
    Par Oluha dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 12/06/2007, 16h50
  3. Réponses: 1
    Dernier message: 28/03/2007, 17h09
  4. [PDO] et bindValue qui ne semble pas fonctionner
    Par Tommyl dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 31/10/2006, 16h12
  5. Ma table temporaire ne semble pas fonctionner.
    Par outshined dans le forum Requêtes
    Réponses: 2
    Dernier message: 12/09/2006, 14h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo