How to get current Connection object in Spring JDBC
Asked Answered
A

4

49

How can I get the current Connection object for an Oracle database? I'm using the JDBC module in Spring 3.0.5.

Anodize answered 8/12, 2011 at 8:35 Comment(0)
I
34

Obtain the Connection from the DataSource bean.

You can access the dataSource by using Spring dependency injection to inject it into your bean, or by accessing ApplicationContext statically:

DataSource ds = (DataSource)ApplicationContextProvider.getApplicationContext().getBean("dataSource");
Connection c = ds.getConnection();
Interpolation answered 8/12, 2011 at 8:49 Comment(5)
But it doesn't return current connection but new connection. I need to return current connection. Situation is: I created connection and i need it to use in another place but in same connectionAnodize
How did you create the connection? Please paste the source.Interpolation
I created connection using instance of Spring JDBC templateAnodize
JDBCTemplate internally uses DataSourceUtils.getConnection() each time you run one of its methods. It doesn't keep a reference to the connection used last time, so I don't think there is a way to get it.Interpolation
@Anodize Please share.Preterhuman
D
34

Just an Info : I am using Spring JDBC Template, which holds the current connection object for me, which can be received as follows.

Connection con;
con = getJdbcTemplate().getDataSource().getConnection();
Dorn answered 27/12, 2011 at 18:39 Comment(3)
For those that may not know, a JdbcTemplate can be injected as field easily: @Autowired JdbcTemplate jdbcTemplate;Acetanilide
conn must be closed after you use it, or you need to create spring transaction and use DataSourceUtils.getConnection()Soak
@DánielKis your comment helped me to use same transaction connection across for one of my scenario. Thanks.Condensed
P
29

Use DataSourceUtils.getConnection().

It returns connection associated with the current transaction, if any.

Pyaemia answered 8/12, 2011 at 9:29 Comment(6)
I don't use transaction. Your piece of code above creates another instance of connection and it's problem for me. I need current instance which i created soonerAnodize
@user330847: If so, what do you mean by "current connection" and what exactly do you want to achieve?Pyaemia
I want to call Oracle stored procedure. I successfully created connection by creating JDBC template instance. After that I execute callable statement and it return me Result Set. Now, I'd like to parse result set and map it to value object. And in this process of parsing I need to get connection which was created in begining. This connection is 'current connection'. I do not know how to get this current connectionAnodize
@user330847: Why do you need the same connection, if you don't need to share transaction associated with that connection?Pyaemia
This method doesn't exist in Spring 5.Redstone
It seems to exist, @RedstoneLui
P
7

I'm not sure if this method was available when this question was originally posted, however, it seems the preferred way to do it in the latest version of Spring is with JdbcTemplate and PreparedStatementCreator. See https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html#query-org.springframework.jdbc.core.PreparedStatementCreator-org.springframework.jdbc.core.PreparedStatementSetter-org.springframework.jdbc.core.ResultSetExtractor- or any of the other query methods that take a PreparedStatementCreator as the first param:

jdbcTemplate.query(con -> {
  // add required logic here
  return con.prepareStatement("sql");
 }, rs -> {
       //process row
});

This has the advantage over the other provided answers (DataSourceUtils.getConnection() or jdbcTemplate.getDataSource().getConnection() as a new connection is not allocated, it uses the same connection management it would as calling any of the other jdbcTemplate querying methods. You also therefore do not need to worry about closing / releasing the connection, since spring will handle it.

Padriac answered 17/5, 2019 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.