I am using Jdbc template and DataSource to connect mysql.
the configuration that I am using is given in application.properties
spring.datasource.url=jdbc:mysql://hostname/databasename
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-active=1
And My Main Class in Which all configurations are called is below:
package com.es.producer;
import javax.jms.ConnectionFactory;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import com.es.producer.config.Config;
import com.rabbitmq.jms.admin.RMQConnectionFactory;
@EnableWebSecurity
@SpringBootApplication
@SpringBootConfiguration
@ComponentScan("com.es.producer")
@EnableAutoConfiguration
@EnableSwagger2
public class EsProducerApplication {
@Autowired
DataSource dataSource;
JdbcTemplate template;
EsProducerApplication() {
}
@Bean
ConnectionFactory connectionFactory() {
RMQConnectionFactory connectionFactory = new RMQConnectionFactory();
connectionFactory.setUsername(Config.getRabbitmqusername());
connectionFactory.setPassword(Config.getRabbitmqpassword());
connectionFactory.setVirtualHost(Config.getRabbitmqvhost());
connectionFactory.setHost(Config.getRabbitmqhost());
return connectionFactory;
}
@Bean
public JdbcTemplate connection(){
return template = new JdbcTemplate(dataSource);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(EsProducerApplication.class, args);
}
}
and The Controller class is below:
package com.es.producer.controller;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.es.producer.EsProducerApplication;
@EnableAutoConfiguration
@Controller
public class DBTOQueueController {
JdbcTemplate template;
@Autowired
JmsTemplate jmsTemplate;
@Autowired
EsProducerApplication esProducerApplication;
@PostConstruct
public void init() {
template =esProducerApplication.connection();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@CrossOrigin
@RequestMapping(method = RequestMethod.POST, path = "/senddata")
@ResponseBody
public ResponseEntity<String> sendDataQueue(
@RequestHeader("tableName") String tableName) throws JSONException {
try {
String selectSql = "select * from " + tableName;
List<Map<String, Object>> employees = template.queryForList(selectSql);
// doing some operations
}}
when I am starting my spring application and after one minute if I try to terminate my application then connection with sql gets released.But if the application is in running state for more minutes and trying to terminate it the termination takes place but the connection is not released from mysql.
Any Help will be appreciated. Thanks.