我在玩交易。
我将 JPA 配置为处理我的实体,并希望将它们保存在数据库中。问题是,当我的程序抛出 runtimeException 时,CMT 不会回滚。
这个“容器的东西”背后的想法很难理解,而且文档记录也很差。
@transactional 托管事务、纯 CDI 拦截器托管事务和 Bean 托管事务的工作就像一个魅力。
这是我编码的内容:这是一个简单的“电影演示”。你想看两部 X 座位的电影。座位有限。如果电影中没有足够的席位,则不应该有交易(ACID 之类的)
在我的 buyTicketsBoundary 课程中:
起初我告诉我的班级我们正在使用 CMT:
@Named("buchungBoundry")
@RequestScoped
@TransactionManagement(TransactionManagementType.CONTAINER)
public class BuchungBoundry {
@EJB
private BuchungVerwaltung buyTicketsController;
@EJB
private KundenVerwaltung customerController;
@EJB
private SaalVerwaltung roomController;
@EJB
private MovieVerwaltung movieController;
private int ticketsForMovie1; //this is how many tickets we want to buy
private int ticketsForMovie2;
public BuchungBoundry() {
}
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void buyTickets() throws MyException {
int numberOfSeantsInRoom1=roomController.getSaal(4).getAnzahlPlaetze();
int numberOfSeantsInRoom2=roomController.getSaal(5).getAnzahlPlaetze();
int alreadyBoughtSeatsIn1=buyTicketsController.getBelegtePlatzeNachSaal(4);
int alreadyBoughtSeatsIn2=buyTicketsController.getBelegtePlatzeNachSaal(5);
int availableSeats1 = numberOfSeantsInRoom1 - alreadyBoughtSeatsIn1;
int availableSeats2 = numberOfSeantsInRoom2 - alreadyBoughtSeatsIn2;
System.out.println("Saal A: ("+alreadyBoughtSeatsIn1+"/"+numberOfSeantsInRoom1+") want to buy :" + ticketsForMovie1);
System.out.println("Saal B: ("+alreadyBoughtSeatsIn2+"/"+numberOfSeantsInRoom2+") want to buy :" + ticketsForMovie2);
try {
if (ticketsForMovie1 <= availableSeats1) {
buyTicketsController.erstelleBuchung(customerController.getKunde(1),
movieController.getMovie(7),
roomController.getSaal(4),
ticketsForMovie1);
} else {
throw new MyException("ERROR: no room for "
+ ticketsForMovie1 + " person! "
+alreadyBoughtSeatsIn1);
}
if (ticketsForMovie2 <= availableSeats2) {
buyTicketsController.erstelleBuchung(customerController.getKunde(1),
movieController.getMovie(8),
roomController.getSaal(5),
ticketsForMovie2);
} else {
throw new MyException("ERROR: no room for "
+ ticketsForMovie2 + " person! "
+alreadyBoughtSeatsIn2);
}
} catch (MyException | IllegalStateException | SecurityException ex) {
Logger.getLogger(BuchungBoundry.class.getName()).log(Level.SEVERE, null, ex);
}
}
我的例外:
@ApplicationException(rollback=true)
public class MyException extends RuntimeException{
public MyException() {
}
public MyException(String s) {
System.out.println(""+s);
}
}
通过 JPA 在数据库中写入已购买票证的控制器类:
@Stateless
public class BuchungVerwaltung implements Serializable {
@PersistenceContext(unitName = "kbse-JTA")
private EntityManager em;
public BuchungVerwaltung() {
}
public void erstelleBuchung(Kunde k, Movie movie, Saal saal, int anzahlPlaetze) {
Buchung b = new Buchung(k, movie, saal, anzahlPlaetze);
em.persist(b);
}
public int getBelegtePlatzeNachSaal(int id) {
try {
Query query = em.createNativeQuery("select sum(b.ANZAHL) from SAAL s,BUCHUNG b where s.SAAL_ID = b.SAAL_SAAL_ID and s.SAAL_ID=?");
query.setParameter(1, id);
return (int) query.getSingleResult();
} catch (Exception e) {
return 0;
}
}
public List<Buchung> getAlleBuchungen() {
TypedQuery<Buchung> query = em.createQuery("select s from Buchung s", Buchung.class);
return query.getResultList();
}
}
问题是,当此类达到第一部电影的 em.persist 状态,而第二部电影抛出异常时,数据库无法回滚。
我想如果抛出 RuntimeException,容器会回滚
我该怎么做才能让它发挥作用?
如果有不清楚的地方请告诉我