I'm working on Spring Boot Application and I use Spring Data, HikariCP and JDBC, but I have a problem.
Inside one method I get a particular User
from the database using a Spring Data repository. After I get the User
from the DB I use JdbcTemplate.query
to get some other information from the DB with username
of above User
but the application freezes and after some time it throws
java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30006ms.
When I debug the code I see that jdbctemplate
is using hikariCP as datasource
.
This is the code I'm using:
public User getUser() {
User user = userRepository.findByUsernameAndEnabledTrue("username");
List<String> roles= getUserRoles(user.getUsername())
return user;
}
private List<String> getUserRoles(String username) {
List<String> roles = this.jdbcTemplate.query("SELECT ga.authority FROM group_authorities ga INNER JOIN group_members gm ON gm.group_id = ga.group_id INNER JOIN users u ON gm.username=u.username WHERE u.username=?;",
new Object[]{username},new ResultSetExtractor<List<String>>() {
@Override
public List<String> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
List<String> roles = new ArrayList<>();
while (resultSet.next()) {
roles.add(resultSet.getString("authority"));
}
return roles;
}
});
return roles;
}
I made a research how to use those together and share same transaction or something like but unfortunately can't fix it.