How to reuse the same connection with a Spring's JdbcTemplate?

后端 未结 6 678
轮回少年
轮回少年 2020-12-13 07:02

I have the following code:


    @Test
    public void springTest() throws SQLException{
        //Connect to the DB.
        DriverManagerDataSource dataSou         


        
6条回答
  •  暖寄归人
    2020-12-13 07:17

    Here's an example using Apache DBCP:-

    BasicDataSource dbcp = new BasicDataSource();
    dbcp.setDriverClassName("com.mysql.jdbc.Driver");
    dbcp.setUrl("jdbc:mysql://localhost/test");
    dbcp.setUsername("");
    dbcp.setPassword("");
    
    JdbcTemplate jt = new JdbcTemplate(dbcp);
    jt.execute("SELECT 1");
    jt.execute("SELECT 1");
    

    The log4j output is:-

    [DEBUG] [JdbcTemplate] [execute:416] - Executing SQL statement [SELECT 1]
    [DEBUG] [DataSourceUtils] [doGetConnection:110] - Fetching JDBC Connection from DataSource
    [DEBUG] [DataSourceUtils] [doReleaseConnection:332] - Returning JDBC Connection to DataSource
    [DEBUG] [JdbcTemplate] [execute:416] - Executing SQL statement [SELECT 1]
    [DEBUG] [DataSourceUtils] [doGetConnection:110] - Fetching JDBC Connection from DataSource
    [DEBUG] [DataSourceUtils] [doReleaseConnection:332] - Returning JDBC Connection to DataSource
    

提交回复
热议问题