1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstart.cdi.veto;
18
19 import java.util.logging.Logger;
20
21 import javax.enterprise.inject.Produces;
22 import javax.inject.Inject;
23 import javax.persistence.EntityManager;
24 import javax.transaction.HeuristicMixedException;
25 import javax.transaction.HeuristicRollbackException;
26 import javax.transaction.NotSupportedException;
27 import javax.transaction.RollbackException;
28 import javax.transaction.SystemException;
29 import javax.transaction.UserTransaction;
30
31 import org.jboss.as.quickstart.cdi.veto.model.Car;
32
33
34
35
36 public class CarManager {
37 @Inject
38 private EntityManager em;
39
40 @Inject
41 private UserTransaction utx;
42
43 private Long id;
44
45 private Logger log = Logger.getLogger(CarManager.class.getSimpleName());
46
47 public void setId(Long id) {
48 this.id = id;
49 }
50
51
52
53
54
55
56 @Produces
57 public Car getCar() {
58 if (id == null) {
59 log.info("Returning new instance of Car");
60 return new Car();
61 }
62 log.info("Finding instance of Car with id " + id);
63 return em.find(Car.class, id);
64 }
65
66 public void save(Car car) {
67 try {
68 utx.begin();
69 em.joinTransaction();
70 em.persist(car);
71 utx.commit();
72 } catch (NotSupportedException e) {
73 log.severe("Transaction Error: " + e.getMessage());
74 } catch (SystemException e) {
75 log.severe("Transaction Error: " + e.getMessage());
76 } catch (HeuristicRollbackException e) {
77 log.severe("Transaction Error: " + e.getMessage());
78 } catch (RollbackException e) {
79 log.severe("Transaction Error: " + e.getMessage());
80 } catch (HeuristicMixedException e) {
81 log.severe("Transaction Error: " + e.getMessage());
82 }
83 }
84 }