Announcement Announcement Module
Collapse
No announcement yet.
Debugging and Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Debugging and Spring

    As i am learning spring, i get more fascinated by it's layered architecture. But one question arises that i would like to share with this forum.


    How do i isolate where is the problem when i debug my spring application using Hibernate as ORM on JBoss server.

    Say for example, i get an HibernateQueryException. How will i know if the problem is happening at the spring level or hibernate or even in JBoss. As we add more layers to our application, i was wondering if it would consume more time to debug hard to detect bugs???

    Any input on this is greatly appreciated.

    Thanks![/quote]

  • #2
    Let's consider you're building a JBoss + Hibernate + properly layered architecture application without Spring. How would you handle it then? Chances are the same techniques can be used.

    I don't use JBoss anymore but I'd imagine it has debugger integration with popular IDEs. That way you can do breakpoints etc.

    Or, the way 95% of all problems are resolved (at least in my experience) is through proper review of log messages. Spring publishes good log messages when its framework classes pick up an error. They're typically sufficient to resolve the problem - or at least post to a Spring forum and get some help. Don't forget you can also publish your own log messages via Commons Logging. There's no reason to not use:

    Code:
    if (logger.isDebugEnabled()) {
       logger.debug("some message");
    }
    The conditional block ensures the runtime performance is logging is disabled for that particular class at that particular level is so marginal you can safely leave such logging messages in the code at all times.

    Comment


    • #3
      Does spring writes into any log file or can i turn on the log to see how Spring handles Session/Transaction when i debug? Say for examples, i've the following piece of code:

      Code:
      	public Collection findProjects(String projectname) throws DataAccessException {
      		return getHibernateTemplate().find("from Project p where p.name = ?", projectname);
      		/*
      If i get HibernateQueryException when i run this query, can i see the actual HQL/SQL query that's used? If yes, how can i do that. Also, Spring in turn uses hibernate. In this case, if there's a bug in Spring or Hibernate, how will i precisely know where is the problem?

      Or am i asking some trivial question?

      Thanks!

      Comment


      • #4
        Spring uses commons-logging which in turn can use a specific logging implementation.

        So, when you set up the logging configuration, you will get what you need. For example, you can set Spring specific logging level to debug when needed while other log 'categories' are at a different logging level.

        Also when really stumped, if you have the Spring source, you can step into that code by using an IDE or a suitable debugger.

        Comment


        • #5
          Originally posted by spring04
          can i see the actual HQL/SQL query that's used? If yes, how can i do that.
          Against your org.springframework.orm.hibernate.LocalSessionFact oryBean bean you can have a list of hibernateProperties defined. Just add one like so:

          <props>
          <prop key="hibernate.dialect">${hibernate.dialect}</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.a uto}</prop>
          </props>

          Comment


          • #6
            Thanks Ben. I'll try this out.

            If i'm running my spring/hibernate app on JBoss, it writes into the server log but for test application (stand-alone JUnit tests), were will it write the log file?

            Thanks again!

            Comment


            • #7
              Take a look in the Petclinic shipped with Spring. In the web.xml you'll notice a log4jConfigLocation which points to WEB-INF/log4j.properties. The web.xml context parameter webAppRootKey is used in the log4j.properties file. Also note the org.springframework.web.util.Log4jConfigListener web.xml listener - disabled by default (you'll want to enable it). Next add log4j.jar to your WEB-INF/lib and you should find logging messages outputted to ${petclinic.root}/WEB-INF/petclinic.log or wherever else you specified.

              Comment


              • #8
                Thanks Ben.

                My application is NOT a web-application. I don't have any web.xml file. Also, as of now, i'm just testing the spring application using JUnit tests.

                In this case, i added the log4j.properties file, added the log4j.jar file in the class path.

                Code:
                # For JBoss&#58; Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
                
                # For all other servers&#58; Comment out the Log4J listener in web.xml to activate Log4J.
                
                log4j.rootLogger=DEBUG stdout, logfile
                
                log4j.appender.stdout=org.apache.log4j.ConsoleAppender
                
                log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
                
                log4j.appender.stdout.layout.ConversionPattern=%d %p &#91;%c&#93; - <%m>%n
                
                
                
                log4j.appender.logfile=org.apache.log4j.RollingFileAppender
                
                log4j.appender.logfile.File=/irmt/src/META-INF/irmt.log
                
                log4j.appender.logfile.MaxFileSize=512KB
                
                # Keep three backup files.
                
                log4j.appender.logfile.MaxBackupIndex=3
                
                # Pattern to output&#58; date priority &#91;category&#93; - message
                
                log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
                
                log4j.appender.logfile.layout.ConversionPattern=%d %p &#91;%c&#93; - %m%n
                I also wrote the following code in my JUnit test to write the log.

                Code:
                public class ProjectTests extends AbstractRMTTests &#123;
                
                	private static Log logger = LogFactory.getLog&#40;ProjectTests.class&#41;;
                
                ....
                ...
                
                       public void testMethod1 &#123;
                                logger.debug&#40;"testmessage"&#41;;
                
                             ...
                       &#125;
                &#125;
                But i DON'T know how to enable this log as you mentioned for this kind of stand-alone application.

                Could you please let me know how to do this?

                Thanks!

                Comment


                • #9
                  You shouldn't need to "enable" it. Having your log4j.jar and log4j.properties in the classpath should suffice. Just looking over your log4j.properties, I think it's missing a comma. eg:

                  log4j.rootLogger=WARN, stdout, fileout

                  Comment


                  • #10
                    Thanks Ben

                    Comment

                    Working...
                    X