我正在学习使用Wildfly Java EE7开发的Java EE Development。现在我遇到了问题,如果我想运行示例代码,我会收到错误:
EJB调用在组件上的失败方法theatreBox for方法public void de.wflydevelopment.chapter4.boundary.TheatreBox.buyTicket(int):javax.ejb.EJBTransactionRolledbackException
到目前为止,我无法修复问题而且我没有想法会导致此错误。我已经将示例代码与我的相比较,但没有运气。
Select sum(sumTotal) as mySum,
case when sum(sumTotal)>25 then 'update' ELSE 'NoUpdate' END AS TopStatistics
from customertrans
where DateTime >= DATEPART(HOUR, '6:00')
package de.wflydevelopment.chapter4.control;
import java.util.Collection;
import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.inject.Inject;
import org.jboss.logging.Logger;
import de.wflydevelopment.chapter4.boundary.TheatreBox;
import de.wflydevelopment.chapter4.entity.Seat;
@Stateless
public class AutomaticSellerService {
@Inject
private Logger logger;
@Inject
private TheatreBox theatreBox;
@Resource
private TimerService timerService;
@Schedule(hour = "*", minute = "*", second = "*/30", persistent = false)
public void automaticCustomer() {
final Seat seat = findFreeSeat();
if(seat == null) {
cancelTimers();
logger.info("Scheduler gone!");
return;
}
theatreBox.buyTicket(seat.getId()); //I think this line causes the error
logger.info("Somebody just booked seat number " + seat.getId());
}
private Seat findFreeSeat(){
final Collection<Seat> list = theatreBox.getSeats();
for(Seat seat : list) {
if(!seat.isBooked()) {
return seat;
}
}
return null;
}
private void cancelTimers() {
for(Timer timer : timerService.getTimers()) {
timer.cancel();
}
}
}
在package de.wflydevelopment.chapter4.boundary;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.ejb.AccessTimeout;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import org.jboss.logging.Logger;
import de.wflydevelopment.chapter4.entity.Seat;
@Singleton
@Startup
@AccessTimeout(value = 5, unit = TimeUnit.MINUTES)
public class TheatreBox {
@Inject
private Logger logger;
private Map<Integer, Seat> seats;
@Inject
private Event<Seat> seatEvent;
@PostConstruct
public void setupTheatre() {
seats = new HashMap<>();
int id = 0;
for (int i = 0; i < 5; i++) {
addSeat(new Seat(++id, "Stalls", 40));
addSeat(new Seat(++id, "Circle", 20));
addSeat(new Seat(++id, "Balcony", 10));
}
logger.info("Seat Map constructed.");
}
private void addSeat(Seat seat) {
seats.put(seat.getId(), seat);
}
@Lock(LockType.READ)
public Collection<Seat> getSeats() {
return Collections.unmodifiableCollection(seats.values());
}
@Lock(LockType.READ)
public int getSeatPrice(int seatId) {
return getSeat(seatId).getPrice();
}
@Lock(LockType.WRITE)
public void buyTicket(int seatId) {
final Seat seat = getSeat(seatId);
final Seat bookedSeat = seat.getBookedSeat();
addSeat(bookedSeat);
seatEvent.fire(bookedSeat);
}
@Lock()
private Seat getSeat(int seatId) {
final Seat seat = seats.get(seatId);
return seat;
}
}
我可以使用findFreeSeat()
但是当我尝试使用theatreBox
我的应用程序崩溃时。删除此行也会删除错误。
theatreBox.buyTicket(seat.getId());
导致seatEvent.fire(bookedSeat);
theatreBox.buyTicket(seat.getId())
package de.wflydevelopment.chapter4.controller;
import java.io.Serializable;
import javax.enterprise.event.Observes;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import de.wflydevelopment.chapter4.entity.Seat;
@Named
@ViewScoped
public class BookingRecord implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int bookedCount = 0;
public int getBookedCount() {
return bookedCount;
}
public void bookEvent(@Observes Seat bookedSeat) {
bookedCount++;
}
}