使用Wildfly学习Java EE7时遇EJBTransactionRolledbackException错误求助
Hey there, sorry to hear you're stuck on this EJB issue while working through the Wildfly Java EE7 book—those transaction rollback exceptions can be tricky since they often hide the real root cause. Let's break down how to dig into this:
First, get the full stack trace from Wildfly's logs
Thejavax.ejb.EJBTransactionRolledbackExceptionis usually a wrapper for the actual error that triggered the rollback. Check your Wildfly server logs (look instandalone/log/server.logby default) and find theCaused by:section further down the stack. That's where you'll see the real exception—like anEntityNotFoundException(if the seat ID you're passing doesn't exist),SQLIntegrityConstraintViolationException(if you're trying to buy an already sold seat), or even aNullPointerExceptionin your business logic.Verify transaction settings on the EJB method
Double-check thebuyTicketmethod's transaction annotations. Does it have@TransactionAttributeset to something unexpected? For example, if it's marked asTransactionAttributeType.READ_ONLYbut you're trying to update seat status, that would trigger a rollback. Also confirm that your EJB class is properly annotated (like@Statelessor@Stateful) as expected in the book's example.Validate the input and underlying data
Make sure the integer ID you're passing tobuyTicketcorresponds to an existing seat in your database. Did you run the data initialization scripts (if the book includes them) to populate the TheatreBox with seats? Also, check if there's any concurrency issue—if multiple requests are trying to buy the same seat at once, an optimistic lock exception might be triggering the rollback.Check persistence layer configuration
Confirm yourpersistence.xmlis correctly set up: does the persistence unit name match what's used in your@PersistenceContextannotation in TheatreBox? Is your Wildfly datasource configured properly (checkstandalone/configuration/standalone.xmlfor the correct JNDI name and database connection details)? A broken database connection or misconfigured persistence unit can silently cause transaction rollbacks.Double-check tiny code details you might have missed
Even if you think your code matches the example, look for small discrepancies: is the seat entity class annotated correctly with@Entityand@Id? Does thebuyTicketmethod correctly retrieve the seat entity before updating its status? Sometimes a missingem.merge()or incorrect entity state handling can lead to unexpected rollbacks.
Once you get that specific root exception from the logs, it'll be way easier to pinpoint the exact issue. Feel free to share that stack trace if you need help interpreting it!
内容的提问来源于stack exchange,提问作者Mark

