How to capture Sybase RAISERROR in Mybatis-Spring? - spring

I am trying to capture error messages in my Java code from stored procedures in Sybase using RAISERROR, but nothing is being caught, even though when I call the proc directly I can see the error is thrown.
I understand that Mybatis-Spring
translates MyBatis exceptions into Spring DataAccessException
So I have coded my Mapper class thusly:
void insertData(Data toInsert) throws Exception, DataAccessException;
I'm trying to catch both of these exceptions, but nothing is caught.
Does anyone have any ideas?
Thanks,
Stephen

I found out how to do it:
try {
myMapper.insertData(data);
}
catch (org.springframework.jdbc.UncategorizedSQLException e) {
Throwable ee = use.getCause();
// getMessage on chained exception gets message from RAISERROR
log.error(ee.getMessage(), e);
}
Cheers,
Stephen

Related

How to simply requeue a RabbitMQ message in case of code exception with SpringBoot

I would like to learn an easy way to requeue a RabbitMQ if an exception is thrown in an SpringBoot application.
#RabbitListener(queues = TRANSACTION_171_REQUEST_QUEUE, errorHandler = "receiverExceptionHandler" )
public void listen171RequestsQueue(Transaction171Request request) {
try {
Transaction171Response response = null;
send171Response("OK", request.getNumeroFormularioRenach());
} catch (Exception e){
//Requeue message
}
}
My code behaviour is to consume a message and take it out of the queue independing of what it happens. I would like to requeue message in RabbitMQ if an exception is thrown.
Could you help me?
I am working in a SpringBoot application with Java 11.
RabbitMQ's default behavior is to requeue when there is an unhandled exception. In your case, you could remove the try..catch block or in the catch block just re-throw the exception.

How to trap error with a transactional annotation?

I use spring boot 2.2.
In a method with transactional anocation, when I save via repository if there is no error, I want to send a message with rabbit mq.
How to be sure there is no error with repository?
#Transactional
public void save(CreditEvent creditEvent) {
repository.save(creditEvent);
//no error send message
}
if there is an error when sending message, I don't want to rollback saving operation.
Although it's Transactional and JPA, still it's a java method which if save failed then unchecked DataAccessException exception will be thrown and flow won't continue to send message.
class is a runtime exception, there is no need for user code to catch it or subclasses if any error is to be considered fatal (the usual case).
#Transactional
public void save(CreditEvent creditEvent) {
try {
repository.save(creditEvent);
//no error send message}
catch {
// send message
// rethrow error
}
}

if the exceptions have been swallowed, how does the spring aop log the exceptions' messages?

Here is my example bellow
public void test() throws Exception {
try {
int i = 1/0;
System.out.println(i);
} catch (Exception e) {
//the exception have been swallowed.
}
}
and the problem is spring aop's AfterThrowing can't work for this. if i remove the try-catch block.it works well then. how can i solve this problem. thanks for any suggestions.
Although it's unclear from question what you intend to achieve but this is the intended behavior of #AfterThrowing advice.
From docs -
After throwing advice runs when a matched method execution exits by throwing an exception.
which means that the advice will be executed only if an exception is thrown from method. In your sample the the exception is consumed and method exits normally thus no advice execution occurs.

getting remote exception while calling an ejb3 method

method(){
try{
some code..
}
catch(Exception e)
{
throw new userDefineException();
}
}
// while calling the above method from the java client I am gettine remote exception but i am expecting to get UserdefineException.
EJB container will wrap undeclared (system) exceptions in RemoteException (or EJBException for local views). To avoid this, you should either:
Change UserDefineException to extend Exception rather than RuntimeException, and add UserDefineException to the throws clause of the remote interface.
Annotation UserDefineException with #ApplicationException, or specify it as <application-exception>com.example.UserDefineException</application-exception> in ejb-jar.xml.

Using Spring AOP, How can I intercept a exception just when it occurred?

Some code like this:
public class A {
#Autoware
private B b;
public void a() {
//AAA: some logic process that maybe throw exception
b.b();
}
}
public class B {
public void b() {
//BBB: some logic process maybe also throw exception
}
}
Both exceptions in A.a() and B.b() need to be intercept, so i use #AfterThrowing annotation do it. but the question is, when i call A.a() in other code and exception has occurred in B.b(), the Advice will execute twice! because exception that occurred in B.b() was propagating to its caller A.a().
I can't swallow the exception silently, because i use spring-amqp, above codes is on Consumer side, i need some message processing that based on the exceptions that occurred in Consumer.
#Around does not work too since i can't swallow the throwed exception.
So, How can i intercept a exception just when it occurred? ignore propagation of it.
Any reply is greatly appreciated.

Resources