Reputation: 333
After successfully running application, I'm facing a connection error. My code is following. Am I using efficient connection handling, or do I need to improve it? Where can I saw my active, used and idle connection status while running application?
@Service
public class printChallan extends JdbcDaoSupport{
@Autowired
DataSource dataSource;
@PostConstruct
private void initialize() {setDataSource(dataSource);}
public boolean printAdmissionForm(HttpServletRequest request)
{
java.sql.Connection conn = null;
try {
conn = getJdbcTemplate().getDataSource().getConnection();
//conn = dataSource.getConnection();
jasperdesign = JRXmlLoader.load(totalPath);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperdesign);
jasperPrint = JasperFillManager.fillReport(jasperReport, param, conn);
} catch (Exception e)
{
if(conn != null) conn.close(); // Does I need to close Connection Manually
e.printStackTrace();
}
return true;
}
public boolean callProcedure(HttpServletRequest request){
CallableStatement cs = null;
try
{
cs = getJdbcTemplate().getDataSource().getConnection() // does this connection need to be closed.
.prepareCall("{? = call FUNC_CHALLAN_PRINT_TYPE(?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.setString(2, challanNbr);
cs.execute();
Status = cs.getString(1);
cs.close();
return Status;
} catch (SQLException e) {
logger.debug("Error occured while getting Challan record" + e.getMessage());
}finally
{
}
return true;
}
}
Application.properties
spring.datasource.url=jdbc:oracle:thin:@172.19.10.37:1521:ORCL
spring.datasource.username=app
spring.datasource.password=app
spring.datasource.platform=ORACLE
Upvotes: 0
Views: 997
Reputation: 58772
If you aren't closing connections, the pool will be full and you will get timeout when trying to get new connection.
You aren't closing connection, simply change to try-with-resources so it'll be closed automatically
try (java.sql.Connection conn = getJdbcTemplate().getDataSource().getConnection()) {
Same solution for CallableStatement
Upvotes: 1