tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cris Berneburg - US <>
Subject Need help understanding DB connection versus servlet request life cycle
Date Fri, 14 Aug 2015 14:12:17 GMT
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?

OS - Windows 7 Pro SP1 64-Bit
IDE - IntelliJ Idea 12.1.6
TOMCAT - 6.0.37

OS - Windows Server 2012 R2 Standard 64-Bit
TOMCAT - 6.0.37

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:
For additional commands, e-mail:

View raw message