How to get current connection in Spring JDBC?

Obtain the Connection from the DataSource bean.

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().

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 – user330847 Dec 8 '11 at 9:25 How did you create the connection?

Please paste the source. – socha23 Dec 8 '11 at 9:38 I created connection using instance of Spring JDBC template – user330847 Dec 8 '11 at 9:57 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 '11 at 10:32 Thanks, I got it – user330847 Dec 8 '11 at 12:52.

Use DataSourceUtils.getConnection(). It returns connection associated with the current transaction, if any.

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 – user330847 Dec 8 '11 at 9:35 @user330847: If so, what do you mean by "current connection" and what exactly do you want to achieve?

– axtavt Dec 8 '11 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 – user330847 Dec 8 '11 at 9:55 @user330847: Why do you need the same connection, if you don't need to share transaction associated with that connection? – axtavt Dec 8 '11 at 10:02.

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().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions