在继承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.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) { }
即此时并没有真正关闭实际的连接。
即谁打开的连接谁负责关闭。
相关推荐
把JDBC应用改成JPA程序,需要把原来通过JDBC API访问数据库的代码替换成使用JPA代码。 JDBC访问数据库的主要工作包括:  得到JDBC驱动程序;  使用DriverManager,Connection,Statement,ResultSet等...
selenium jpa jdbc入门搭建
Spring Boot 集成 JPASpring Boot 集成 JPASpring Boot 集成 JPASpring Boot 集成 JPASpring Boot 集成 JPASpring Boot 集成 JPA
JPA JDBC hibernate mybatis对比,里面从官方例子比较详细对比说明,从开发效率,crud执行效率等各种维度上做对比分析
打印完整带参数sql,不用自己去替换问号,后端省时省力。
jpa 脚本现实
jpa测试用例jpa测试用例jpa测试用例
spring boot,持久层的使用实例,其中使用了JPA 、Hibernate、Spring Data、Spring Data JPA、JdbcTemplate
1:多租户系统集成 2:集成shardingjdbc分库分表 3:集成shardingjdbc读写分离 4:集成人大金仓数据库 5:重写了JPA-saveandflush方法 6:修复了JPA更新数据为null的问题 7:解压后导入idea,创建数据库即可运行测试
使用ssh搭建框架+jpa+注解的使用 和通用Dao的使用集成SSH框架
jar包,官方版本,自测可用
简单实用的Spring集成Jpa实例,20分钟教会你
SpringBoot+JPA+Freemarker 生成动态SQL,通过Test下的 junit Test运行代码
Springboot2+JPA|MyBatis+Sharding-JDBC示例源码,可直接运行。
融合JPA功能和最佳的sql编写及查询模式、独创的缓存翻译、最优化的分页、并提供无限层级分组汇总、同比环比、行列转换、树形排序汇总、sql自适配不同数据库、分库分表、多租户、数据加解密、脱敏以及面向复杂业务和...
Spring4集成JPA和MyBatis3简单例子
针对spring boot 集成jpa sql操作进行自定义分库分表逻辑
EJB3JPA调用原生SQL和函数存储过程[归类].pdf
Spring集成JPA和MyBatis简单例子
spring boot+jpa+sqlserver+bootstrap案例 利于学习开发