drenda
drenda

Reputation: 6254

Spring jpa saving data during rollback

I know that sounds strange the question but the reasoning is quite simple. I've an server side app developed with Spring+Hibernate.

I've a custom servlet:

@Transactional
@Component
public class ReceivingSms implements HttpRequestHandler {
...
...
...
@Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ....reading data from parameters and store into the db....
    entityManager.persist(someEntities);

    //at some point I found that there is some error and I've to rollback the entire transaction
    if(error){
        //HERE I WANT TO SAVE A LOG ON DB
        throw new RuntimeException("Error");
    }
}

The reason is this:

From here my question: there is a way to store an entity in the db and rollback the rest of a transaction?

Thanks

Upvotes: 0

Views: 917

Answers (1)

JB Nizet
JB Nizet

Reputation: 692161

Call another transactional service, whose method is annotated with

@Transactional(propagation = Propagation.REQUIRES_NEW)

to save the log entry.

Upvotes: 2

Related Questions