emacarron / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

ErrorContext leaks in ThreadLocal #104

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
MyBatis 3.0.2 on Tomcat 6.0.29:
After undeploying my mybatis-guice web app, an instance of ErrorContext is 
still alive - see screenshot from YourKit Profiler in attachment.

This results in the complete PermGen leaking (ErrorContext -> ClassLoader -> 
Classes).
I can only redeploy my app a couple of times before I have to restart Tomcat, 
which means writing an urgent mail to the server team and hoping for a quick 
response.

Original issue reported on code.google.com by stephen....@gmail.com on 14 Sep 2010 at 9:12

Attachments:

GoogleCodeExporter commented 9 years ago
Turned out it was a problem with mybatis-guice (ErrorContext implictly created 
in ConfigurationProvider and never reset).
See the attached patch (also posted in the forum thread discussion with Simo).
(Patch also contains a fix for the other mentioned problem with custom 
TypeHandlers not working dues to wrong order of calls to @Inject setters.)

Original comment by stephen....@gmail.com on 15 Sep 2010 at 12:26

Attachments:

GoogleCodeExporter commented 9 years ago

Original comment by simone.t...@gmail.com on 15 Sep 2010 at 7:41

GoogleCodeExporter commented 9 years ago
Your patch has been sucefully applied, thanks Stephen.
I added trivial modifications to your original code since I'm not a very big 
fun of classes declared on-the-fly, BTW I maintained your original logic.
I hope you'll enjoy seeing your nome on the maven pom.

Margin note: the RC2 will be released today so you can upgrade it in your 
system.

Original comment by simone.t...@gmail.com on 15 Sep 2010 at 8:18

GoogleCodeExporter commented 9 years ago
I am observing the same issue with a mybatis-spring webapp,
using mybatis 3.0.3, mybatis-spring-1.0.0-RC3.

The culprit is SqlSessionFactoryBean.buildSqlSessionFactory() (invoked from 
SqlSessionFactoryBean.afterPropertiesSet()).

The bean is defined as following:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceData" />
    <property name="configLocation" value="/WEB-INF/sqlmap-config.xml" />
</bean>

When buildSqlSessionFactory() parses the config file with XMLConfigBuilder, the 
builder creates the ErrorContext. The ErrorContext.reset() method is never 
called.

If I specified the "mapperLocations" property to SqlSessionFactoryBean, I'd 
expect to observe the same behavior caused by XMLMapperBuilder.

My workaround is to subclass SqlSessionFactoryBean and reset the ErrorContext 
when afterPropertiesSet() call returns:

public class CustomSqlSessionFactoryBean extends SqlSessionFactoryBean {
  @Override
  public void afterPropertiesSet() throws Exception {
    try {
      super.afterPropertiesSet();
    } finally {
      ErrorContext.instance().reset();
    }
  }
}

Original comment by knst.kol...@gmail.com on 2 Dec 2010 at 6:45

GoogleCodeExporter commented 9 years ago
Sorry, I have just seen this issue.
Having a look at it I would say that normally the call to 
sqlSessionFactoryBuilder.build() should reset ErrorContext but certainly if an 
exception is thrown in the middle of the method it wont be called. 
Anyway it is better to close it explicitly.
Fixed in r3468. Please give it a try.

Original comment by eduardo.macarron on 31 Dec 2010 at 12:03

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 5 Apr 2012 at 6:27