Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

jdbc

I have the following code:

      @Test     public void springTest() throws SQLException{         //Connect to the DB.         DriverManagerDataSource dataSource = new DriverManagerDataSource();         dataSource.setDriverClassName("org.h2.Driver");         dataSource.setUrl("jdbc:h2:/data/h2/testa");         dataSource.setUsername("");         dataSource.setPassword("");         JdbcTemplate jt=new JdbcTemplate(dataSource);         jt.execute("SELECT 1");         jt.execute("SELECT 1");     }  

I expect the two execute() lines to reuse the same connection. However, the log output says:

 2011-02-10 12:24:17 DriverManagerDataSource [INFO] Loaded JDBC driver: org.h2.Driver 2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1] 2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource 2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa] 2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource 2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1] 2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource 2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa] 2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource 

The above example runs quite fast but I have a larger piece of code that does basically the same thing and hangs for a long time on Creating new JDBC DriverManager Connection. I never get an error but it makes the code run very slowly. Can I somehow refactor the above code to just use the same connection?

Thanks

like image 448
User1 Avatar asked Feb 10 '11 18:02

User1


People also ask

Does JdbcTemplate use connection pooling?

Spring Example JDBC Database Connection PoolJdbcTemplate requires a DataSource which is javax. sql. DataSource implementation and you can get this directly using spring bean configuration or by using JNDI if you are using the J2EE web server or application server for managing Connection Pool.

Does spring JdbcTemplate close connection?

In short yes it does close the connection.

Does spring JdbcTemplate automatically clean up resources?

The Spring JDBC Template has the following advantages compared with standard JDBC. The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections.

What is the difference between JdbcTemplate and HibernateTemplate?

HibernateTemplate v/s JdbcTemplate Spring provides support for both hibernate and JDBC template classes. It provides template classes which contains all common code. But JDBC as we all know is not an ORM tool it does not represent rows as objects whereas Hibernate does that.


2 Answers

Spring provides a special DataSource that allows you to do this: SingleConnectionDataSource

Changing your code to this should do the trick:

SingleConnectionDataSource dataSource = new SingleConnectionDataSource(); .... // The rest stays as is 

For use in multi-threaded applications, you can make the code re-entrant by borrowing a new connection from the pool and wrapping it around the database-intensive section of code:

// ... this code may be invoked in multiple threads simultaneously ...  try(Connection conn = dao.getDataSource().getConnection()) {     JdbcTemplate db = new JdbcTemplate(new SingleConnectionDataSource(conn, true));      // ... database-intensive code goes here ...      // ... this code also is safe to run simultaneously in multiple threads ...     // ... provided you are not creating new threads inside here } 
like image 87
Axel Fontaine Avatar answered Oct 13 '22 06:10

Axel Fontaine


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 
like image 35
limc Avatar answered Oct 13 '22 06:10

limc