Chris, Why don't you allow tomcat manage the connection for you?
http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html It is a lot easier that way. Do you need examples? On Fri, Aug 14, 2015 at 9:12 AM, Cris Berneburg - US <cberneb...@caci.com> wrote: > BACKGROUND: I've been doing Java servlet coding for about 2 years and > need help understanding something. I work on a legacy JSP and servlets web > application project using Tomcat. Previously all the SQL was embedded > right into the Java and JSP code. I added the myBatis framework and moved > all the SQL to mappers. Also previously, every SQL statement had its own > separate DB connection instantiated and opened but not closed. I changed > that so each servlet request would have only one open DB connection by > adding a ServletRequestListener that opens the sqlSession in > requestInitialized and closes it in requestDestroyed. > > PROBLEM: When running in my IDE in my dev environment, various random > myBatis calls in the JSP pages would fail. After one would fail, all the > rest would fail too. There were 3 repeated phrases - NullPointerException, > PersistenceException, and ExecutorException Executor was closed. > > DEBUGGING: It appeared the DB connection was being closed before the page > was finished rendering. That meant the MyBatis ServletRequestListener > requestDestroyed handler was being triggered before the JSP page was > finished being served. When I commented out the sqlSession.close statement > the JSP page worked OK. > > WORK-AROUND: I added a keepConnectionOpen flag in my development > configuration file and coded the ServletRequestListener requestDestroyed > handler to not close the sqlSession if the flag was true. The JSP page now > loads without error. > > NOTE 1: While there being only one open SQL session per servlet request > open at a time is an improvement, it bothers me that the DB connection is > not being explicitly closed. > > NOTE 2: The problem does not currently happen in our test environment. > > NOTE 3: Our web site has low traffic volume. > > QUESTION(s) 1: Why is the ServletRequestListener requestDestroyed handler > triggered before the JSP page is finished? Is it due to my IDE or Tomcat > or something else? My interpretation is that the IDE is handling the > processing differently than Tomcat somehow, although I don't understand > that since the IDE is invoking Tomcat. Should I be concerned that the > problem will eventually happen in my test and future production > environments? > > QUESTION 2: Is there a better way to manage DB connections anyway? > > DEVELOPMENT ENVIRONMENT: > OS - Windows 7 Pro SP1 64-Bit > IDE - IntelliJ Idea 12.1.6 > TOMCAT - 6.0.37 > JAVA - JDK 1.6.0.24 > > TEST ENVIRONMENT: > OS - Windows Server 2012 R2 Standard 64-Bit > TOMCAT - 6.0.37 > JAVA - JRE 1.8.0.45 > > NOTE 4: My customer uses Tomcat 6, so I must use that too. > > You read down this far? Thanks for your time, help, and encouragement. > :-) > > -- > Cris Berneburg, Lead Software Engineer, CACI > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > -- Thanks, Osagie Uwaifo