Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9265155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:59:52+00:00 2026-06-18T13:59:52+00:00

Hi I am trying to log exception into the DB public void a(){ try{

  • 0

Hi I am trying to log exception into the DB

public void a(){
        try{

              String c = b();
           }catch (Throwable ex){
                   com.log.Logger.log(ex);
                   throw new UserDefinedException(ex);
           }
        }

public String b(){
               throw new NullPointerException("Transaction Logger")
}

I have a LoggerImpl class which logs the details of the exception into DB.
Only the UserDefinedException is getting logged where as the Null Pointer Exception is not. Could any one Plz help me.

LogEntry.java

private long id;
private String desc;

// getters and setters

Logger.java

public long log(Throwble ex){

    LogEntry entry = new LogEntry();
    entry.setDesc(ex.getMessage());
    LoggerImpl log = new LoggerImpl();
    log.insertLog(entry);
    return entry.getId();
}

LoggerImpl.java

@Transactional(propogation = PROPOGATION.REQUIRES_NEW)
public void insertLog(LogEntry log){
    insert.update(//fields);
}

Id is generated using a Sequence Incrementer

I am using JTA Transaction Manager.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:59:54+00:00Added an answer on June 18, 2026 at 1:59 pm

    You got such results because in your case LoggerImplementation class isn’t managed by Spring container and container doesn’t start new transaction for insertLog(..) method (as it supposed to be). To make this class managed you should inject it in your bean. I propose you to make such refactoring. This will work.

    Instead of having Logger and LoggerImpl classes create Logger interface and LoggerImpl class that implements this interface.

    public interface Logger {
    
        long log(Throwable ex);
    
    }
    
    @Transactional
    @Component
    public final class LoggerImpl implements Logger {
    
        @PersistenceContext
        private EntityManager entityManager;
    
        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public long log(Throwable ex) {
            LogEntry entry = new LogEntry();
            entry.setDescription(ex.getMessage());
    
            entityManager.persist(entry);
    
            return entry.getId();
        }
    
    }
    

    And then simply inject your exceptions Logger into necessary service class. For example:

    @Transactional(rollbackFor=Exception.class)
    @Service
    public class TestService {
    
        @Autowired
        private Logger logger;
    
        public void a() throws UserDefinedException {
            try {
    
                b();
            } catch (Throwable ex) {
                logger.log(ex);
                throw new UserDefinedException(ex);
            }
        }
    
        public String b() {
            throw new NullPointerException("Transaction Logger");
        }
    }
    

    Now outer transaction will be rolled back but inner new transaction will write data to DB.

    Note rollbackFor attribute in @Transactional annotation for TestService class. This is necessary because by default Spring will not rollback transaction for chacked exceptions. This described behaviour that you got. In your case outer transaction was rollbacked only for runtime exceptions. That’s why when you try to log NullPointerException the whole transaction is rolled back and your log record isn’t added to the DB. But when you try to log UserDefinedException transaction is successfully commited despite the fact that error have been thrown and your log record is written to the DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to log into google reader using AfNetworking AfHttpClient but I am
I'm trying to log into a site and obtain the code back, but i'm
I am trying to log into a website, then go to a link and
I am trying to log some input values into an array via jquery and
I am trying to log into facebook here is my code: <body> <form id=form1
I am using spring exception translator to wrap java exception into flex exception. public
I am trying to insert into a XpressMP database, It's a new database. I
I am trying to save an Image to Derby Db(JDBC),but it gives exception void
I'm trying to use HtmlUnit in Java to log into a website. First i
I'm trying to write a Java program that can automatically log into Facebook. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.