#database #oracle #spring-boot #hikaricp
Вопрос:
Я пытаюсь подключить базу данных oracle из своего приложения Spring boot с помощью пула соединений Hikari.Сталкиваясь с проблемами подключения большую часть времени по нескольким различным причинам, таким как тайм-ауты подключения, неработающие соединения и т.д., Я опубликовал приведенный ниже код, который был написан для выполнения этой задачи и отслеживания стека ошибок. Пожалуйста, помогите мне понять это и в конечном итоге решить эту проблему.
**Query.Class**
try(Connection conn = DBUtils.INSTANCE.getConnection("Table_A")) {
//prepare statements
}catch(Ex e) {
}
*DBUtils.class**
private final static Map<String,DataSource> dataSourceMap = new ConcurrentHashMap<>(3);
public synchronized static Connection getConnection(String datSourceName) throws ProcessingException, SQLException {
if (!dataSourceMap.containsKey(datSourceName)) {
System.out.println("Getting request for first time for datasource [" datSourceName "]");
switch (DataSourceType.valueOf(datSourceName)) {
case Table_A_Oracle:
getTableAOracle();
break;
case Table_B_Oracle:
getTableBSource();
break;
case Table_C_Oracle:
getTableCSource();
break;
default:
throw new ProcessingException(ErrorCodes.APPLICATION_ERROR, "Unknown data source");
}
}
Connection conn = dataSourceMap.get(datSourceName).getConnection();
return conn;
}
public static DataSource getTableAOracle() throws ProcessingException {
try {
String url = "jdbc:oracle:thin:@com-orcl.XXXX.com:1521/TableA" ;
String username = "XXXXX";
String password = "YYYYY" ;
HikariConfig hikariConfig = new HikariConfig(getDatabaseProperties(Configuration.INSTANCE::TableAProperties));
hikariConfig.setJdbcUrl(url) ;
hikariConfig.setPassword(password) ;
hikariConfig.setUsername(username) ;
DataSource ds = new HikariDataSource(hikariConfig);
dataSourceMap.put("Table_A",ds);
return ds ;
}catch (Throwable e)
{
throw new ProcessingException(ErrorCodes.DATABASE_ERROR, String.format("Unable to create datasource to TableA"),e.getCause(),true);
}
}
private static Properties getDatabaseProperties(Supplier<Map<String, String>> databasePropertiesMap)
{
Properties properties = new Properties();
Map<String, String> map = databasePropertiesMap.get();
map.entrySet().stream()
.peek(entry -> APP_LOGGER.info("DataBaseUtils::getTableADatabaseProperties; Key: [{}] - Value: [{}]", entry::getKey, entry::getValue))
.forEach(entry -> properties.put(entry.getKey(), entry.getValue()));
System.out.println(properties.toString());
return properties;
}
**COnfiguration.class**
public Map<String, String> TableAProperties()
{
return DBApplication.getTableAConfiguration().getConnectionDetails(); /** BEAN for DataBaseConfigurationTableA class
}
**DataBaseConfigurationTableA.class**
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import java.util.Map;
@Configuration
@EnableConfigurationProperties(DataBaseConfigurationTableA.class)
@ConfigurationProperties(prefix = "database.tableA")
public class DataBaseConfigurationCOMTier {
public Map<String, String> getConnectionDetails() {
return connectionDetails;
}
public void setConnectionDetails(Map<String, String> connectionDetails) {
this.connectionDetails = connectionDetails;
}
private static Map<String, String> connectionDetails ;
}
**application.yml** file
database:
tableA:
connectionDetails:
poolName: 'comtier'
connectionInitSql: 'SELECT 1 FROM DUAL'
connectionTestQuery: 'SELECT 1 FROM DUAL'
connectionTimeout: 120000
driverClassName: 'oracle.jdbc.driver.OracleDriver'
idleTimeout: 15000
# initializationFailTimeout: 2000
maximumPoolSize: 10
minimumIdle: 1
readOnly: false
validationTimeout: 10000
maxLifetime: 120000
leakDetectionThreshold: 120000
Error Stack Trace:
2021-09-10 14:42:15.730 ERROR 24524 — [-MessageGroup-1] com.zaxxer.hikari.pool.HikariPool : Exception during pool initialization.
java.sql.SQLRecoverableException: IO Error: Socket read interrupted, connect lapse 44 ms., Authentication lapse 0 ms.
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:794) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:688) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:39) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:691) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:117) ~[HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:123) ~[HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:375) ~[HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:204) ~[HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:445) [HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:516) [HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.pool.HikariPool.(HikariPool.java:116) [HikariCP-2.7.2.jar:na]
at com.zaxxer.hikari.HikariDataSource.(HikariDataSource.java:72) [HikariCP-2.7.2.jar:na]
Caused by: java.io.IOException: Socket read interrupted, connect lapse 44 ms., Authentication lapse 0 ms.
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:790) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
… 59 common frames omitted
Caused by: java.io.IOException: Socket read interrupted, connect lapse 44 ms.
at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:127) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:317) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1438) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:518) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
… 59 common frames omitted
Caused by: java.net.SocketTimeoutException: Socket read interrupted
at oracle.net.nt.TimeoutSocketChannel.read(TimeoutSocketChannel.java:152) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.net.ns.NIOHeader.readHeaderBuffer(NIOHeader.java:82) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.net.ns.NIOPacket.readNIOPacket(NIOPacket.java:252) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
at oracle.net.ns.NSProtocolNIO.negotiateConnection(NSProtocolNIO.java:118) ~[ojdbc8-12.2.0.1.jar:12.2.0.1.0]
… 62 common frames omitted«`