У меня есть две базы данных с Mybatis. У меня есть находка с mybatis. Но иногда у меня есть исключение: SQL state [null]; код ошибки [0]; Заявление закрыто .; вложенное исключение java. sql .SQLException: оператор закрыт. Это не всегда случается.
Исключение
Caused by: org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause: java.sql.SQLException: Statement is closed.
### The error may exist in file [D:\code\xx\target\classes\mapper\Person.xml]
### The error may involve xx.xx.xx.findPersonCnt
### The error occurred while handling results
### SQL: select count(*) from person b
### Cause: java.sql.SQLException: Statement is closed.
; uncategorized SQLException; SQL state [null]; error code [0]; Statement is closed.; nested exception is java.sql.SQLException: Statement is closed.
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy174.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy195.findPersonCnt(Unknown Source)
Один конфиг
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "xx.xx.xx.mapper", sqlSessionFactoryRef = "oneSqlSessionFactory")
public class TwoSourceConfig {
// 将这个对象放入Spring容器中
@Bean(name = "oneDataSource")
@Primary
@ConfigurationProperties(prefix = "one.datasource")
public DataSource getIgniteDateSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oneSqlSessionFactory")
@Primary
// @Qualifier表示查找Spring容器中名字为igniteDataSource的对象
public SqlSessionFactory igniteSqlSessionFactory(@Qualifier("oneDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
@Bean("oneSqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate ignitesqlsessiontemplate(
@Qualifier("igniteSqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
Два конфига
@Configuration
@MapperScan(basePackages = "xx.xx.xx.xx.mapper", sqlSessionFactoryRef = "twoSqlSessionFactory")
public class TwoDataSourceConfig {
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "two.datasource")
public DataSource getDateSource2() {
return DataSourceBuilder.create().build();
}
@Bean(name = "twoSqlSessionFactory")
public SqlSessionFactory tidbSqlSessionFactory(@Qualifier("twoDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/*.xml"));
return bean.getObject();
}
@Bean("twoSqlSessionTemplate")
public SqlSessionTemplate tidbsqlsessiontemplate(
@Qualifier("tidbSqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
Подскажите, пожалуйста, как решить это исключение?