`
jinnianshilongnian
  • 浏览: 21437447 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2405675
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2998213
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5631860
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257683
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1593354
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:249031
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5848024
Group-logo
跟我学Nginx+Lua开...
浏览量:698288
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:780650
社区版块
存档分类
最新评论

混合jpa和jdbc集成测试时Connection第二次执行sql时被关闭原因及解决方案

 
阅读更多

在继承AbstractTransactionalJUnit4SpringContextTests 并使用如下代码进行集成测试时:

 

    @Before
    public void setUp() {
        setSqlScriptEncoding("utf-8");
        executeSqlScript("classpath:sql/intergration-test-data.sql", false);
    }

 可能得到如下异常:

写道
20:53:42.375 [main] WARN o.s.j.support.SQLErrorCodesFactory - Error while extracting database product name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetaData; nested exception is java.sql.SQLException: Couldn't perform the operation getMetaData: You can't perform a getMetaData operation after the connection has been closed

 

写道

org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException for SQL [delete from `sys_job`]; SQL state [null]; error code [0]; Couldn't perform the operation createStatement: You can't perform a createStatement operation after the connection has been closed; nested exception is java.sql.SQLException: Couldn't perform the operation createStatement: You can't perform a createStatement operation after the connection has been closed
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.Jdb

 

这是因为内部的JdbcTemplate在调用DataSourceUtils.releaseConnection(con, getDataSource());释放连接时,是调用的:org.springframework.orm.jpa.vendor.HibernateJpaDialect的HibernateConnectionHandle

 

		public void releaseConnection(Connection con) {
			JdbcUtils.closeConnection(con);
		}
    public static void closeConnection(Connection con) {
		if (con != null) {
			try {
				con.close();
			}
			catch (SQLException ex) {
				logger.debug("Could not close JDBC Connection", ex);
			}
			catch (Throwable ex) {
				// We don't trust the JDBC driver: It might throw RuntimeException or Error.
				logger.debug("Unexpected exception on closing JDBC Connection", ex);
			}
		}
	}

  

 

即con.close();直接关闭连接。

 

此时我们应该使用TransactionAwareDataSourceProxy代理之:

 

<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.logicalcobwebs.proxool.ProxoolDataSource">
                <property name="driver" value="${connection.driver_class}"/>
                <property name="driverUrl" value="${connection.url}"/>
                <property name="user" value="${connection.username}"/>
                <property name="password" value="${connection.password}"/>

                <property name="alias" value="${proxool.alias}"/>
                <property name="trace" value="${proxool.trace}"/>
                <property name="maximumConnectionCount" value="${proxool.maximum.connection.count}"/>
                <property name="minimumConnectionCount" value="${proxool.minimum.connection.count}"/>
                <property name="statistics" value="${proxool.statistics}"/>
                <property name="simultaneousBuildThrottle" value="${proxool.simultaneous.build.throttle}"/>
            </bean>
        </property>
    </bean>

 

 

此时在调用DataSources.doGetConnection获取ConnectionHolder时,内部使用SimpleConnectionHandle而非HibernateConnectionHandle,即releaseConnection并没有释放连接:

        public void releaseConnection(Connection con) {
 	}

 即此时并没有真正关闭实际的连接。 

 

即谁打开的连接谁负责关闭。

9
0
分享到:
评论
1 楼 飞天奔月 2013-05-12  
谁打开的连接谁负责关闭

相关推荐

Global site tag (gtag.js) - Google Analytics