Making jdbc connection and storing in session?
<p>I have implemented logging mechanism in Oracle by the way of a stored procedure. And in java, I am having a class which has different methods that is called from different places of my application. It has constructor which creates sql connection. The methods make calls to the stored procedure with appropriate parameters.
   But once the user comes to the site, I want the jdbc connection to be made to oracle and use the same connection throughout the user session. This is accomplished by putting the connection object into the session. Then by retrieving the connection object from session before any method call is made, I make the object's method call. And I want to close the connection finally when the user session ends.
  Thats why I was trying to do the one I mentioned in my earlier post here at </p>

<p><a href="http://stackoverflow.com/questions/9720784/closing-a-jdbc-connection-object-when-session-expires">Closing a jdbc connection object when session expires</a></p>

<p>To make it clear, plz see the sample code format,</p>

<pre><code>import java.sql.CallableStatement; 
import java.sql.Connection; 
import java.sql.DriverManager; 


public class DbTest {


    private static String url = "jdbc:oracle:thin:@localhost:7001:xe";
    private static String username = "";
    private static String password = "";
    private Connection conn = null;


public DbTest() {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection(url, username, password);
    }


public void log1(String param1, String param2...) {

try {

CallableStatement stmt = conn.prepareCall("{call SP (?, ?)}"); 

stmt.setString(1, param1); 
stmt.setString(2, param2); 
stmt.execute(); 

} catch (SQLException e) { 

e.printStackTrace(); 

} 
}

public void log2(String param1, String param2...) {
...
...
}

} //End of DbTest class


import DbTest;

public class userEnteringClass {

public void caller() {

DbTest dbtest = (DbTest)session.getAttribute("DbTest");
if (dbtest == null) {
dbtest = new dbtest();
session.setAttribute("DbTest", dbtest);
}

....

dbtest.log1(param1, param2);
....
....
dbtest.log2(param1, param2);
...
}
}
</code></pre>

<p>If this is not a good way, then plz suggest a better way. Thanks for any help</p>
