I use oracle.jdbc.pool.OracleDataSource
for Oracle Pooling connections, but it seems the connections stay alive in the database. Today I got an error while connecting to Oracle Database:
ORA-12516: TNS:listener could not find available handler with matching protocol stack
And I was told by our database administrator that too many open connections stay in "IDLE" mode from my application.
I use ojdbc7:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.1</version>
</dependency>
Spring applicationContext.xml file:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="connectionCachingEnabled" value="true" />
</bean>
The DbConnect class:
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class DbConnect {
@Autowired
private DataSource dataSource;
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
Also I use jdk7 try-with-resources syntax in my dao level for connections:
@Autowired
private DbConnect dbConnect;
public List<User> getAllUsers() {
List<User> list = new ArrayList<>();
try(Connection connection = dbConnect.getConnection()) {
try(PreparedStatement preparedStatement = connection.prepareStatement("select * from V_USERS t")) {
try(ResultSet resultSet = preparedStatement.executeQuery()) {
while(resultSet.next()) {
list.add(RowFetcher.fetchUser(resultSet));
}
}
}
}
catch(Exception e) {
log.error(e.getMessage(), e);
}
return list;
}
Cannot figure out where is the actual problem. Should I use c3p0 instead of OracleDataSource? Is there any bug in ojdbc7 or error in my code?
Thanks in advance!
DbConnect
– user180100getAllUsers
though – user180100