Précédent   Forum des professionnels en informatique > Java > Général Java > Persistance > JPA
JPA Forum d'entraide sur l'API de persistance JPA (Java Persistence API)
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 02/02/2012, 17h05   #1
Membre du Club
 
Inscription : avril 2010
Messages : 65
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 65
Points : 41
Points : 41
Par défaut Gestion des transactions : Définir le mode programatique sur une méthode

Bonjour,

Au niveau d'un session bean (stateless) qui fonctionne correctement en mode managé j'ai besoins de gérer manuellement ma transaction pour la méthode supprimerLocalite.

L'objectif est de pouvoir catcher une ConstraintViolationException dans la couche métier, c'est en recherchant dans ce forum des problèmes semblables au mien que j'ai compris que les exceptions lancées lors du comit() ne peuvent pas être attrapées dans la méthode puisqu'elles sont lancées à la fin (en mode managé).

Voici le code :

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
@Stateless
@Remote(ConfigurationBeanRemote.class)
public class ConfigurationBean implements ConfigurationBeanRemote {
 
  @PersistenceContext(unitName = "adressesEJB", type=PersistenceContextType.TRANSACTION)
  EntityManager em;
 
  public ConfigurationBean() {
  }
 
  @Override
  public void modifierLocalite(Localite cetteLocalite) throws InternalErrorException {
 
  try {
    em.merge(cetteLocalite);
  } catch (Exception e) {
    e.printStackTrace();
    throw new InternalErrorException("Erreur lors de modification de cette localité");
  }
}
 
  @TransactionManagement(TransactionManagementType.BEAN)
  public void effacerLocalite(Localite cetteLocalite) throws InternalErrorException {
 
  try {
    em.remove(em.merge(cetteLocalite));
  } catch (ConstraintViolationException e1) {
    e1.printStackTrace();
    throw new InternalErrorException("Cette localité ne peut pas être supprimée");
  } catch (Exception e) {
    e.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  }
 
}
Je précise que du coup je n'ai pas placé les méthodes begin(), joinTransaction() et commit() autour de la suppression

Quand j'utilise, sur la méthode supprimerLocalite l'annotation :
Code :
@TransactionManagement(TransactionManagementType.BEAN)
mon parseur me dit (The annotation @TransactionManagement is disallowed for this location) qu'elle ne peut pas être utilisée à cet endroit et pourtant j'ai lu le contraire.

Voilà j'ai besoins de votre aide à nouveau.
dr-Padbol est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/02/2012, 17h57   #2
Modérateur
 
Inscription : août 2006
Messages : 2 848
Détails du profil
Informations forums :
Inscription : août 2006
Messages : 2 848
Points : 2 950
Points : 2 950
D'après la javadoc :
Citation:
Specifies whether a session bean or message driven bean has container managed transactions or bean managed transactions. If this annotation is not used, the bean is assumed to have container-managed transaction management.
Donc on ne peut pas mélanger les deux types de gestion au sein du même bean.
fr1man est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/02/2012, 18h24   #3
Membre du Club
 
Inscription : avril 2010
Messages : 65
Détails du profil
Informations forums :
Inscription : avril 2010
Messages : 65
Points : 41
Points : 41
Ok, merci pour ta réponse

Voici le code qui fonctionne chez moi maintenant :
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
@Stateless
@Remote(ConfigurationBeanRemote.class)
@TransactionManagement(javax.ejb.TransactionManagementType.BEAN)
public class ConfigurationBean implements ConfigurationBeanRemote, Serializable {
 
  private static final long serialVersionUID = 1L;
  private static final Logger log = Logger.getLogger(ConfigurationBean.class);
 
    @Resource
    private UserTransaction userTx;
 
    private EntityManagerFactory emFactory;
    private EntityManager em;
 
    public ConfigurationBean() {
    }
 
  (...)
 
  @Override
  public void effacerLocalite(Localite cetteLocalite) throws InternalErrorException {
 
  emFactory = Persistence.createEntityManagerFactory("adressesEJB");
  em = emFactory.createEntityManager();
 
  try {
    userTx.begin();
    em.joinTransaction();
    em.remove(em.merge(cetteLocalite));
    userTx.commit();
  } catch (NotSupportedException e1) {
    e1.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  } catch (SystemException e2) {
    e2.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  } catch (SecurityException e3) {
    e3.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  } catch (IllegalStateException e4) {
    e4.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  } catch (RollbackException e5) {
    e5.printStackTrace();
    throw new InternalErrorException("Cette localité ne peut pas être supprimée");
  } catch (HeuristicMixedException e6) {
    e6.printStackTrace();
    throw new InternalErrorException("Cette localité ne peut pas être supprimée");
  } catch (HeuristicRollbackException e7) {
    e7.printStackTrace();
    throw new InternalErrorException("Cette localité ne peut pas être supprimée");
  } catch (Exception e) {
    e.printStackTrace();
    throw new InternalErrorException("Erreur lors de suppression de cette localité");
  }
 
}
 
}
dr-Padbol est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 14h35.


 
 
 
 
Partenaires

Hébergement Web