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