我有一个Java Spring Boot应用程序,它使用Spring JDBC驱动程序将数据批量插入sybase。我正在使用Sybase JDBC驱动程序,并使用DriverManagerDataSource和HikariConnectionPool进行批量插入。数据源的创建可以正常进行,但是当我尝试创建HikariConnectionPool时,它给出了错误的连接关闭错误。下面是代码。如您所见,我正在做一个显式调用,以查看connecton是否关闭并且返回false。我无法理解我在这里缺少什么。 TIA有任何建议。
源代码
@Configuration
@Profile("local")
public class SpringJDBCLocalConfiguration {
Logger logger = LoggerFactory.getLogger(SpringJDBCLocalConfiguration.class);
@Bean(name = "dataSource")
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource(){{
setDriverClassName("com.sybase.jdbc4.jdbc.SybDriver");
setUrl("url");
setUsername("username");
setPassword("passwd");
}};;
try {
Connection conn = dataSource.getConnection();
logger.info("SpringJDBCLocalConfiguration conn "+conn);
logger.info("SpringJDBCLocalConfiguration conn isClosed "+conn.isClosed());
}
catch(Exception exc){
logger.error("SpringJDBCLocalConfiguration error getting connection in SpringJDBCLocalConfiguration "+exc);
}
HikariConfig config = new HikariConfig() {{
setDataSource(dataSource);
setConnectionTestQuery("SELECT 1");
}};
HikariDataSource hikariDataSource = new HikariDataSource(config);
return hikariDataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
return jdbcTemplate;
}
}
错误堆栈跟踪
[ main] c.j.a.w.e.d.SpringJDBCLocalConfiguration : inside SpringJDBCLocalConfiguration.dataSource method flushStrategy
[ main] com.zaxxer.hikari.HikariConfig : sybase-pool - idleTimeout is close to or more than maxLifetime, disabling it.
[ main] com.zaxxer.hikari.HikariDataSource : sybase-pool - Starting...
[ main] com.zaxxer.hikari.pool.PoolBase : sybase-pool - Driver does not support get/set network timeout for connections. (com.sybase.jdbc4.jdbc.SybConnection.getNetworkTimeout()I)
[ main] com.zaxxer.hikari.pool.HikariPool : sybase-pool - Exception during pool initialization.
java.sql.SQLException: JZ0C0: Connection is already closed.
at com.sybase.jdbc4.jdbc.ErrorMessage.raiseError(ErrorMessage.java:779) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybConnection.checkConnection(SybConnection.java:4010) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybStatement.close(SybStatement.java:744) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybStatement.close(SybStatement.java:719) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.MdaManager.loadMetaData(MdaManager.java:573) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.MdaManager.<init>(MdaManager.java:188) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.MdaManager.<init>(MdaManager.java:171) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybConnection.checkMDA(SybConnection.java:4155) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybConnection.getMDA(SybConnection.java:3685) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.tds.Tds.setOption(Tds.java:1829) ~[jconn4-7.07-27307.jar!/:na]
at com.sybase.jdbc4.jdbc.SybConnection.setReadOnly(SybConnection.java:2586) ~[jconn4-7.07-27307.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:409) ~[HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:370) ~[HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194) ~[HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460) [HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534) [HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-2.7.9.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81) [HikariCP-2.7.9.jar!/:na]
答案 0 :(得分:0)
问题是应用程序正在云中运行并且能够建立数据库连接,但是防火墙似乎终止了该连接,因此在创建连接池时未发现活动连接导致此错误。我尝试将IP地址和端口明确列入白名单,但是仍然存在相同问题,但根本原因是防火墙阻止了连接。