Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is oracle.jdbc.OracleCallableStatement thread safe?

 
DeepakN kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using oracle.jdbc.OracleCallableStatement in my code. The question is is this thread safe and can i use the same instance of it in a multi-threaded environment?

In my code, i am declaring a class variable inside IncompatibilityCheckDAOImpl.java as below:

/** OracleCallableStatement */
private OracleCallableStatement cStmt = null;

Now in the method, say execute(), i am doing the following:

if(this.cStmt == null) {
this.cStmt = (OracleCallableStatement)con.prepareCall("{call PKG_INCOMPATIBILITY_CHECK.PROC_CHECK_INCOMPATIBILITY(:1, :2)}");
}

So effectively, only one OracleCallableStatement instance will be created as IncompatibilityCheckDAOImpl.java is created as a singleton object.

My question is if multiple threads share same oracle.jdbc.OracleCallableStatement, will there be thread safety?

I have found somewhere in the net that oracle.jdbc.driver.OracleCallableStatement is thread safe but no one mentioned anything about oracle.jdbc.OracleCallableStatement.
Alos not that i am using it inside websphere.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance of OracleCallableStatement you got is tied to a connection. And only one processing can be active over that connection (an Oracle thick driver probably allows you to use more sessions over a single connection, but I have no idea how to do that and what would the ramifications be; I also assume you're using a thin driver, so I'll stop here), so you cannot ever process two calls in parallel using one OracleCallableStatement instance, even if it was thread-safe. More importantly, you should use the same connection simultaneously in two threads processing two distinct business transactions, since they would share a database transaction and that would be a horrible mess.

So my conclusion is: regardless of whether the OracleCallableStatement is thread-safe, you should always process only one business/database transaction over a single connection and either obtain another connection (and thus another OracleCallableStatement instance) for parallel processing, or use synchronization to process only one transaction at a time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic