This discussion is archived
4 Replies Latest reply: May 25, 2012 8:38 AM by gimbal2 RSS

Using custom listener to initialize databaase connection

895686 Newbie
Currently Being Moderated
Hello,

I am trying to use a custom listener to initialize database connection pool (C3P0) on start up and then destroy on context shut down. The reason for that is that I whenever context is shutdown I have a memory leak because initialized connection pool is not being destroyed.

I have a static class called C3P0Utils that deals with connection pool.

In my listener in contextInitialized method I have tried at first to init the pool like this

+public void contextInitialized(ServletContextEvent sce) {+
+try {+        
C3P0Utils.newInstance().init();
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+

I know that object is created. I checked it using jconsole. However it is not accessible with in the application.

My second attempt was to regester the pool and then add it to the context and then when I need it read from there.

+public void contextInitialized(ServletContextEvent sce) {+
+try {+

ServletContext ctx=sce.getServletContext();
C3P0Utils.newInstance().init();
ctx.setAttribute("myDataSource", C3P0Utils.newInstance().getDataSource());
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+

However when I try to red from the context I get nothing. I try to read like this.

+(ComboPooledDataSource)ctx.getAttribute("myDataSource")+

When I print names of all of the attributes in the context I get these attributes.

Context Name

org.apache.tomcat.InstanceManager

org.apache.catalina.jsp_classpath

javax.servlet.context.tempdir

org.apache.catalina.resources

+org.apache.tomcat.JarScanner

+org.apache.jasper.compiler.TldLocationsCache

+org.springframework.web.context.WebApplicationContext.ROOT


I am lost why I can not use anything initialized in the listener.
  • 1. Re: Using custom listener to initialize databaase connection
    EJP Guru
    Currently Being Moderated
    Does getDataSource() of a non-initialized instance really work? Does it return null by any chance? Or throw an exception?
  • 2. Re: Using custom listener to initialize databaase connection
    895686 Newbie
    Currently Being Moderated
    I found a bug in my own code. I can't believe I missed it :(
  • 3. Re: Using custom listener to initialize databaase connection
    895686 Newbie
    Currently Being Moderated
    It returned null. Anyways I found the problem and it has nothing to do with the listener. I had a bug in my code.
  • 4. Re: Using custom listener to initialize databaase connection
    gimbal2 Guru
    Currently Being Moderated
    I would have expected you to find stuff in your log then. I wonder if:
    Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
    this is actually useful; I'm assuming you're passing null for the log message here, I wonder if the logger will actually do something with a null message. Make it a habit of just logging "SomethingOrAnother didn't work so well!" when you only want to log a caught exception, just in case.


    (btw: use \
     tags to post code or anything else that you want to have formatted neatly)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points