我正在玩交易。
我将JPA配置为处理我的实体,并希望将它们保存在数据库中。问题是,当我的程序抛出runtimeException时,CMT不会回滚。
这个"容器事物背后的想法"很难理解,记录很差。
@transactional托管交易,纯CDI Interceptor托管交易和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在数据库中编写Bought票证的Controller类:
@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,那么Container就会回滚
我能做些什么才能让它发挥作用?
Plase告诉我,如果有什么不清楚的话
答案 0 :(得分:0)
我做了所有的核心,唯一的错误是抓住了#34; myException&#34;
} catch (MyException | IllegalStateException | SecurityException ex) {
} catch ( IllegalStateException | SecurityException ex) {
现在好了,容器可以按照它应该回滚