Spring JdbcTemplate (i) used in spring boot--connection pooling creates multiple connections

Source: Internet
Author: User
Tags connection pooling

This article refers to some articles on the Internet, finally finishing the practice of drawing.

1, create a springboot demo program, you can refer to my article:

Create Springboot Demo Program

2, look at the directory structure:


3, first need to introduce the Pom file dependency:

<!--Https://mvnrepository.com/artifact/mysql/mysql-connector-java-->
        <dependency>
            < groupid>mysql</groupid>
            <artifactId>mysql-connector-java</artifactId>
            <version >8.0.11</version>
        </dependency>
        <!--https://mvnrepository.com/artifact/com.alibaba/ Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactid>druid </artifactId>
            <version>1.1.9</version>
        </dependency>
         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactid>spring-boot-starter-jdbc </artifactId>
        </dependency>

4, add the corresponding connection pool and connection objects in the configuration file Datasourcemysql.xml configuration, at the same time need to add the properties of the corresponding Jdbc.url and so on the specific configuration (here skipped).

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans  Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--data source--> <bean id=" Datasourcepool " class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" > <!--Basic property URL, use R, password--> <property name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${j" Dbc.username} "/> <property name= password" value= "${jdbc.password}"/> <!--configuration initialization size, minimum, maximum
        ; <property name= "InitialSize" value= "1"/> <property name= "Minidle" value= "1"/> <property nam
        E= "maxactive" value= "/> <!--configuration gets connection wait timeout time--> <property name=" maxwait "value=" 10000 "/> <!--configuration RoomHow often does it take a test to detect an idle connection that needs to be turned off, in milliseconds--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> & lt;! --Configures the minimum time to live a connection in a pool, in milliseconds--> <property name= "Minevictableidletimemillis" value= "300000"/> Operty name= "Testonborrow" value= "false"/> <property name= "Testonreturn" value= "false"/> <!-- Open Pscache and specify the size of the Pscache on each connection if you use Oracle, configure Poolpreparedstatements to True,mysql can be configured to False. --> <property name= "Poolpreparedstatements" value= "false"/> <property "name=" Tementperconnectionsize "value="/> </bean> <!--data source--> <bean id= "DataSourcePool2" class = "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "Close" > <!--basic property URL, user, pass Word--> <property name= "url" value= "${jdbc.url2}"/> <property name= "username" value= "${jdbc.u" Sername2} "/> <property name=" Password "Value= "${jdbc.password2}"/> <!--configuration initialization size, minimum, maximum--> <property name= "InitialSize" value= "1"/> <property name= "Minidle" value= "1"/> <property name= "maxactive" value= "/>" <!-- Configure the time to get the connection wait timeout--> <property name= "maxwait" value= "10000"/> <!--how long does it take to configure the interval to detect the idle connections that need to be closed, the unit is no SEC--> <property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!--Configure a connection to the minimum surviving time in the pool, unit is Millisecond--> <property name= "Minevictableidletimemillis" value= "300000"/> <property name= "TestOnBorr" ow "value= false"/> <property name= "Testonreturn" value= "false"/> <!--open Pscache, and specify PSCA on each connection If the size of Che is in Oracle, the poolpreparedstatements is configured to True,mysql to be configured to False. --> <property name= "Poolpreparedstatements" value= "false"/> <property "name=" Tementperconnectionsize "value=" "/> </bean> <bean id=" remOtejdbctemplate "class=" org.springframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" Dataso Urcepool "></property> </bean> <bean id=" localjdbctemplate "class=" Org.springframework.jdbc.core . JdbcTemplate "> <property name=" dataSource "ref=" dataSourcePool2 "></property> </bean> &L T;/beans>

5. Inject two JDBC connection objects into your code (note here that the name of the variable must be the same as the ID of the bean declared in the XML, otherwise you will need to add a note on @resource to specify name)

6, at the same time need to add the following in the Springboot startup type:

@SpringBootApplication (exclude = {Datasourceautoconfiguration.class})
@ImportResource (locations = {"Classpath: Datasourcemysql.xml "})

The effect of this is to prevent springboot automatic binding DBC configuration from generating errors. Another is to add the bean declared in Datasourcemysql.xml to the Springboot scan range.

7, the use of the time is like this.

@Resource
    private JdbcTemplate remotejdbctemplate;


    @Resource
    private JdbcTemplate localjdbctemplate;

8, then you can use the normal.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.