Results 1 to 10 of 10

Thread: Debugging and Spring

  1. #1
    Join Date
    Nov 2004
    Posts
    159

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    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.

  3. #3
    Join Date
    Nov 2004
    Posts
    159

    Default

    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!

  4. #4
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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.

  5. #5
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    Quote 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>

  6. #6
    Join Date
    Nov 2004
    Posts
    159

    Default

    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!

  7. #7
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    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.

  8. #8
    Join Date
    Nov 2004
    Posts
    159

    Default

    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!

  9. #9
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    2,768

    Default

    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

  10. #10
    Join Date
    Nov 2004
    Posts
    159

    Default

    Thanks Ben

Similar Threads

  1. How to turn off Spring logging when in JUnit?
    By dhicks in forum Container
    Replies: 7
    Last Post: Apr 23rd, 2008, 03:17 PM
  2. Spring re-creates singleton beans
    By drc in forum Container
    Replies: 5
    Last Post: Jul 15th, 2005, 07:12 AM
  3. Replies: 4
    Last Post: May 20th, 2005, 03:20 AM
  4. Spring debugging question : IoC
    By etienno in forum Architecture
    Replies: 7
    Last Post: Apr 13th, 2005, 08:26 AM
  5. Debugging spring applications
    By spring04 in forum Architecture
    Replies: 1
    Last Post: Nov 17th, 2004, 02:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •