49

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

4 Answers 4

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();
5
  • 20
    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 8, 2011 at 9:25
  • How did you create the connection? Please paste the source.
    – socha23
    Dec 8, 2011 at 9:38
  • I created connection using instance of Spring JDBC template
    – vlcik
    Dec 8, 2011 at 9:57
  • 3
    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 8, 2011 at 10:32
  • @vlcik Please share. Sep 22, 2017 at 5:31
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();
2
  • 8
    For those that may not know, a JdbcTemplate can be injected as field easily: @Autowired JdbcTemplate jdbcTemplate;
    – acdcjunior
    Nov 24, 2017 at 21:58
  • conn must be closed after you use it, or you need to create spring transaction and use DataSourceUtils.getConnection() Feb 4, 2022 at 7:25
29

Use DataSourceUtils.getConnection().

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

6
  • 1
    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 8, 2011 at 9:35
  • @user330847: If so, what do you mean by "current connection" and what exactly do you want to achieve?
    – axtavt
    Dec 8, 2011 at 9: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 8, 2011 at 9:55
  • 2
    @user330847: Why do you need the same connection, if you don't need to share transaction associated with that connection?
    – axtavt
    Dec 8, 2011 at 10:02
  • 5
    This method doesn't exist in Spring 5.
    – OrangeDog
    Mar 19, 2019 at 12: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.