Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to use Prepared Statements with DBCP Pooled Connections?

843859Jun 2 2008 — edited Nov 20 2014
Hi all,

i have a lack of understand how it works. I Hope someone can explain it.

Let us start with Prepared Statements first. I think all of you know them. To achieve performance gain they have to let open, just passing new parameters.So the first time i use them i have to create it from a SQL-Connection.
PreparedStatement pstmt = con.prepareStatement("SELECT * from foo where id=?")
For reusing i usually would cache or save them in my application. I dont close them.

The Specification of DBCP said, they support PreparedStatements in pooled Connections.
Really Nice. But, Uuhm? How it works?

In my application i can call a Connection from the Pool and release them with closing it.
Unfortunatly my PreparedStatement depends on the connection and will released too. Right?
Connection con = DataSourceX.getConnection();
PreparedStatement pstmt = con.prepareStatement("");
con.close;

//Next Call of the Connection:
con = DataSourceX.getConnection();
//what now?
pstmt = con.prepareStatement("")
I assume this code will create a new PreparedStatement. The advantage of performance and recycling would be gone, until i doesn't know how to access the previous prepared Statement?

The Documentation on DBCP or GOOGLE haven't help me. I hope you can. :-)

Thanks a lot!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 30 2008
Added on Jun 2 2008
3 comments
527 views