Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on EJB 3 transaction

 
Javeed Pasha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

Currently I am learning EJB 3 from Enterprise JavaBeans, 3.0
By Bill Burke, Richard Monson-Haefel

I am follwing the worked examples in this book , the task flow is first user will select available cabin id , cruise id along with his customer id to book a reservation. in travael agent bean it will check it has created all three objects and books the reservation .

when i start the App server and run the task flow it works for the first time but not subsequently it throwa the error Null Pointer exception
java.lang.NullPointerException
com.titan.clients.BookingTravel.doGet(BookingTravel.java:64)
com.titan.clients.BookingTravel.doPost(BookingTravel.java:140)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


Excerpts from my code

in servlet :
dao = ctx.lookup("titan/TravelAgentBean/remote");
dao.setCruiseID(CRUISE_ID);
dao.setCabinID(cabinsId);
Customer cust =dao.findOrCreateCustomer(first,last);
ticket = dao.bookPassage(card, amount);
out.println(ticket.toString());
dao.checkout();

In TravelAgentBean EJB:
@Stateful
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class TravelAgentBean implements TravelAgentRemote {

public TicketDO bookPassage(CreditCardDO card, double price)
throws IncompleteConversationalState
{
System.out.println("Customer Name :"+ customer.getFirstName());
// System.out.println("CruiseID:"+ cruise.getId());
System.out.println("cabin ID "+ cabin.getId());
if (customer == null || cruise == null || cabin == null)
{
throw new IncompleteConversationalState( );
}

Reservation reservation = new Reservation(customer, cruise, cabin, price, new Date( ));
entityManager.persist(reservation);

Query getCruiseCabin = entityManager.createQuery("SELECT cc FROM CruiseCabin cc WHERE " +
"cc.cabin = :cabin AND cc.cruise = :cruise");
getCruiseCabin.setParameter("cabin", cabin);
getCruiseCabin.setParameter("cruise", cruise);
CruiseCabin cc = (CruiseCabin)getCruiseCabin.getSingleResult();

if (cc.getIsReserved())
throw new EJBException ("Cabin is already reserved");
cc.setIsReserved(true);

try
{
processPayment.byCredit(customer, card, price);

TicketDO ticket = new TicketDO(customer, cruise, cabin, price);
return ticket;
}
catch(Exception e)
{
throw new EJBException(e);
}
@Remove
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void checkout()
{
// this really isn't necessary as the entityManager would
// be enlisted with the transaction anyways.
entityManager.flush();
}
}


Please Help On this

Cheers!
JP
 
Joe Harry
Ranch Hand
Posts: 10087
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the entire source code? Also make sure that you use the format tags for the source code otherwise finding help form the ranch would be remote.
 
Javeed Pasha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jothi Shankar

I have attached the source code please check.

Regards
JP
 
Joe Harry
Ranch Hand
Posts: 10087
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your BookingTravel.java?
 
Joe Harry
Ranch Hand
Posts: 10087
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how do I know which is line number 64?
 
Javeed Pasha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BookingTravel.java


TravelAgentBean.java

processpayment.java
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line number of your BookingTravel.java seems to be shifted by 2 lines, so I believe line 64 is actually the following code.



You said that it worked for the first time and failed in subsequent accesses. It is because your code calls the checkout before exiting the doGet. This will clear the dao (instance variable). That's why, in subsequent accesses, it throws the NullPointerException.


BTW, you should avoid using instance variable in your servlet class. Servlet implementation, by default, must be thread-safe.
 
Javeed Pasha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have annotated the checkout method of TravelAgent Bean as @Remove. i have to call this method to persist the changes to DB .

if i comment out dao=null; in BookingTravel.java it throws
"javax.ejb.NoSuchEJBException: Could not find stateful bean: 5c4o1f-k9sxmq-gcvrfblt-1-gcvrhkk3-c (bean was marked as removed"

qUESTION : As per my understanding the above exceptio is occured because of annotating the checkout() method as @Remove.

How can i access this stateful bean again?
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your EJB is a stateful bean, so after the removal you will receive NoSuchEJBException when accessing the handler again.

Basically, you cannot do the lookup inside the init(ServletConfig config) method, as it gets called only once during the lifecycle of the servlet. Please move the lookup code and the dao inside the doGet method.
 
Javeed Pasha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks K it worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic