在spring启动应用程序中一段时间​​后无法从mysql释放连接

时间:2017-05-31 12:26:13

标签: mysql spring spring-boot datasource jdbctemplate

我正在使用Jdbc模板和DataSource来连接mysql。

我正在使用的配置在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

我的主类在其中调用所有配置如下:

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);

    }
}

和Controller类如下:

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

}}

当我开始我的spring应用程序时,如果我尝试终止我的应用程序,那么在一分钟之后就会释放与sql的连接。但是如果应用程序处于运行状态更长时间并尝试终止它,则终止发生但是连接不是从mysql释放的。

任何帮助将不胜感激。 感谢。

0 个答案:

没有答案