wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Question on EJB 3  transaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question on EJB 3  transaction" Watch "Question on EJB 3  transaction" New topic
Author

Question on EJB 3 transaction

Javeed Pasha
Greenhorn

Joined: Aug 02, 2010
Posts: 5
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

Joined: Sep 26, 2006
Posts: 9345
    
    2

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.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Javeed Pasha
Greenhorn

Joined: Aug 02, 2010
Posts: 5
Hi Jothi Shankar

I have attached the source code please check.

Regards
JP
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Where is your BookingTravel.java?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

And how do I know which is line number 64?
Javeed Pasha
Greenhorn

Joined: Aug 02, 2010
Posts: 5

BookingTravel.java


TravelAgentBean.java

processpayment.java
k space
Ranch Hand

Joined: Jun 25, 2002
Posts: 104
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.

SCEA | SCBCD | SCWCD | SCJP - The kSpace
Javeed Pasha
Greenhorn

Joined: Aug 02, 2010
Posts: 5
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

Joined: Jun 25, 2002
Posts: 104
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

Joined: Aug 02, 2010
Posts: 5
Thanks K it worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on EJB 3 transaction
 
Similar Threads
NamingException: Could not dereference object - EJB 3.0 workbook ex05-2
Unable to understand exception
JMS confusions
JPA using Hibernate - Entity Manager doubts
ejb 3.0 lookup