How Can I Access the Current Database Connection Object?

Send me a mail
 Dive into Oracle ADF   Click to see the XML version of this web page.   (Updated: 2/3/2008; 9:26:18 PM.)
Tips and tricks from Steve Muench on Oracle ADF Framework and JDeveloper IDE

How Can I Access the Current Database Connection Object?

Tobias writes in with a question. "For our custom reporting solution based on generating PDF files using Apache FOP, we need to access the current JDBC connection that BC4J is using. How can we access it?"

You could use a technique like this:

/**
* Put this method in your XXXXImpl.java class where you need
* to access the current JDBC connection
*/
private Connection getCurrentConnection() throws SQLException {
/* Note that we never execute this statement, so no commit is really happening */
PreparedStatement st = getDBTransaction().createPreparedStatement("commit",1);
  Connection conn = st.getConnection();
  st.close();
  return conn;
}

CODER CAVEAT: Since there is no guarantee that your application may be using the exact same application module instance across different web page requests (and hence, highly likely that it won't be using the same exact JDBC connection object each time), obviously it is not wise to cache the JDBC connection in your own code anywhere.



© Copyright 2008 Steve Muench. Click here to send an email to the editor of this weblog.
Last update: 2/3/2008; 9:26:18 PM.