hbwf / mybatis

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

Invocation of destroy method 'close' failed on bean with name 'sqlSessionTemplate' #778

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
MyBatis3.2.0+Spring3.2.1(MyBatis3.1.1 is ok)
2013-02-20 11:53:54 - [ WARN ]  Invocation of destroy method 'close' failed on 
bean with name 'sqlSessionTemplate'
java.lang.UnsupportedOperationException: Manual close is not allowed over a 
Spring managed SqlSession
    at org.mybatis.spring.SqlSessionTemplate.close(SqlSessionTemplate.java:306)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:320)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:246)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:510)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:486)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:455)
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1090)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1064)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:980)
2013-02-20 11:53:54 - [ DEBUG ]  Invoking destroy method 'close' on bean with 
name 'dataSource'

Original issue reported on code.google.com by cobweb1...@gmail.com on 20 Feb 2013 at 3:59

GoogleCodeExporter commented 9 years ago
Seems that Spring tries to close the session because he finds that SqlSession 
implements closeable. 

But, if I am not wrong, this is just a warning. Isn't it?

Original comment by eduardo.macarron on 25 Feb 2013 at 9:23

GoogleCodeExporter commented 9 years ago
source
MyBatis3.1.1 :public abstract interface SqlSession extends Closeable
MyBatis3.2.0 :public abstract interface SqlSession

Original comment by cobweb1...@gmail.com on 26 Feb 2013 at 10:52

GoogleCodeExporter commented 9 years ago
I do know that, it was me who did the change! :)

The purpose of that change is to support java 7 try-with-resources when using 
an SqlSession.

As a side effect, it looks like spring 3.2 (this does not happen in 3.1) 
recognizes the interface and tries to close a managed SqlSession 
(SqlSessionTemplate) that cannot be closed so it throws an exception indicating 
it.

Spring caches the exception so no error reaches de application code.

I was about to nop the .close() in the template to remove the warning but 
thought that is is better not to do it because people may think that calling 
.close() over a SqlSessionTemplate does something and it does not. So I think 
is better that Spring keeps complaining and people know for sure that a 
SqlSessionTemplate cannot be closed manually.

Original comment by eduardo.macarron on 27 Feb 2013 at 5:47

GoogleCodeExporter commented 9 years ago

Original comment by eduardo.macarron on 5 Mar 2013 at 6:34

GoogleCodeExporter commented 9 years ago
While I understand Eduardo's comment, I think there should be a way to turn off 
the warning. When I write new code, I expect no error and no warning: I want 
clean code and the current behavior made jump in my code looking for a fix. I 
found nothing, so I starting googling around until I found this issue. 
Everybody with the same line of though as me will do the same so we will loose 
time for nothing.
Maybe there should be a better message ?
Maybe a method could added to request ignoring silently the close call ?
I would certainly use such method from my junit to make sure there is no 
useless warning.

Original comment by MarcGagn...@gmail.com on 12 Dec 2013 at 2:09

GoogleCodeExporter commented 9 years ago
I agree that it is disconcerting to see that error message in the logs.  Not 
really a fix fix, but here's a workaround that I use to avoid seeing that:

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
      <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

Original comment by james.s....@gmail.com on 6 Aug 2014 at 7:34