47

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

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
vlcik
  • 911
  • 4
  • 20
  • 33

4 Answers4

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();
socha23
  • 10,171
  • 2
  • 28
  • 25
  • 19
    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 connection – vlcik Dec 08 '11 at 09:25
  • How did you create the connection? Please paste the source. – socha23 Dec 08 '11 at 09:38
  • I created connection using instance of Spring JDBC template – vlcik Dec 08 '11 at 09:57
  • 2
    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. – socha23 Dec 08 '11 at 10:32
  • @vlcik Please share. – coderatchet Sep 22 '17 at 05:31
33

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();
Arun GK
  • 538
  • 3
  • 10
  • 8
    For those that may not know, a `JdbcTemplate` can be injected as field easily: `@Autowired JdbcTemplate jdbcTemplate;` – acdcjunior Nov 24 '17 at 21:58
  • conn must be closed after you use it, or you need to create spring transaction and use DataSourceUtils.getConnection() – Dániel Kis Feb 04 '22 at 07:25
28

Use DataSourceUtils.getConnection().

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

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 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 sooner – vlcik Dec 08 '11 at 09:35
  • @user330847: If so, what do you mean by "current connection" and what exactly do you want to achieve? – axtavt Dec 08 '11 at 09:48
  • 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 connection – vlcik Dec 08 '11 at 09:55
  • 2
    @user330847: Why do you need the same connection, if you don't need to share transaction associated with that connection? – axtavt Dec 08 '11 at 10:02
  • 5
    This method doesn't exist in Spring 5. – OrangeDog Mar 19 '19 at 12:26
  • It [seems to exist](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/datasource/DataSourceUtils.html#getConnection-javax.sql.DataSource-), @OrangeDog – hd1 May 22 '22 at 19:26
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.

aarbor
  • 1,398
  • 1
  • 9
  • 24