aspose file tools*
The moose likes Web Services and the fly likes SQL exception not handled in WebService Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "SQL exception not handled in WebService" Watch "SQL exception not handled in WebService" New topic
Author

SQL exception not handled in WebService

sreedhar sri
Greenhorn

Joined: Sep 05, 2012
Posts: 9
Hi All,

We developed the stateless Web-service implementation. We used JPA as ORM layer to do the database operations. In service methods, we used the Entity Manager to persist the entity. while persisting the same record into the table, we are not able to handle those exception "Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (TIGOSUSCRIPTIONES.SYS_C0020549) violated" exception in Service method. It is directly throwing the following exception in client result.

Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Transaction rolled back
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:107)
at $Proxy30.registerSaleOutcome(Unknown Source)

How to handle this exception. Following is the code we used in service method business logic.


@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void recycleOperation(Recycle recycle) throws RecycleFault_Exception{
try{
em.persist(recycle);
} catch(Exception e){
//not coming to this block
log.error("Exception in Data Insertion:"+e.getMessage());
RecycleFault fault = new RecycleFault();
fault.setErrorCode("101");
fault.setMessage("Record is already Existed");
RecycleFault_Exception faultExp = new RecycleFault_Exception("RecycleFault Exception", fault);
throw faultExp;
}
}

Could you help me to solve this problem.

Thanks in Advance.

Sreedhar.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
throwing some logical idea, may not hit the target.

Try putting a log or sop statement at end of method out side catch. something like, log.warn("I visited here but not the catch....so catch me if you can....");

let me know if this warning is printed....


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
sreedhar sri
Greenhorn

Joined: Sep 05, 2012
Posts: 9
Yes. I used log.warn("some text"); out side catch in the method. It is printed the log message. But dint entered into catch block.

Could you tell me how to handle that exception.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
okay so thing is, this exception is not thrown when em.persist(recycle); is invoked but its thrown during commit that happens after method execution is complete.
so you need to handle such cases using exception handlers following link gives good example on how to register handlers (interceptors) for ejb

Handlers for EJB 3.x
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
of course the easiest option is to use APO i.e write a method like handleException and annotate it with @ArroundInvoke.
but this option is good if you have couple of services else it violates DRY principle.
sreedhar sri
Greenhorn

Joined: Sep 05, 2012
Posts: 9
In this service implementation, we integrated other external services. So in this case which is better to use to handle those exceptions.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
my personal preference is to decouple the concerns and have generic approach for things like exception handling, so I would go for writing exception Handlers rather than ArroundInvoke in each service class.
sreedhar sri
Greenhorn

Joined: Sep 05, 2012
Posts: 9
In service class, we have some methods and they throw related Exception classes message. So i have to show the specific exception message to the client. How can i solve this using exception handler approach.
i wrote the ExceptionHanler class, handler chain xml file and pointed that handler chain in ServiceImplementation. So in MyApplicationBaseException class how to specify all the exceptions regarding service methods.

Could you give any suggestion.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
You don't have to specify service method execptions, once you register the handler,handleFault it will be invoked for any exception and you can interrogation fault or exception like


read the link i sent previously, its nice example and should solve all your queries.

-P
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SQL exception not handled in WebService