File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL exception not handled in WebService

 
sreedhar sri
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
sreedhar sri
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 642
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic