Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JDBC connection and storing in session till it expires?

Posted on 2012-03-16
8
Medium Priority
?
231 Views
Last Modified: 2012-08-13
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.

To make it clear, plz see the sample code format,

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);
 ...
 } 
}

Open in new window


Hope you got what I am trying to do.
If this is not a good way, then plz suggest a better way. If possible with a sample code. Thanks for any help
0
Comment
Question by:weblogicme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37728162
perhpas this would help which shows how to call method befioere session expires:

http://stackoverflow.com/questions/3943368/invoke-method-just-before-session-expires
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37728191
I think thsi is on the similar subject, check the accepted solution:

http://stackoverflow.com/questions/7756054/how-to-call-a-method-before-the-session-object-is-destroyed
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37728194
also read comments below the solution (same that I posted above):

http://stackoverflow.com/questions/7756054/how-to-call-a-method-before-the-session-object-is-destroyed

the comments  seem to be useful
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 37728371
User Iterceptor concept ... in that you can check pre- request and post-request also
0
 
LVL 11

Accepted Solution

by:
petiex earned 2000 total points
ID: 37730882
The short answer is, don't do that. Use connection pooling. Whenever a database transaction is required, get a connection from the connection pool, do the transaction in a try block that catches SQL or other exceptions, and close the connection in a finally block to return it to the pool. Whatever you are using as an application- or web- server, google that with "connection pool", and you will likely get a good lead on how to do this.

Under your current scenario, if you have more concurrent sessions than available db connections, you run out of connections. It seems that a single user can exhaust your database by logging in and closing his browser repeatedly, because there's really no way of knowing when somebody ends a session (e.g., by closing the browser) before the session expires. Multiple abandoned sessions = multiple open connections = overburdened db server and angry DBAs.
0
 

Author Comment

by:weblogicme
ID: 37733123
So I can implement connection pooling as mentioned here?
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html

It mentiones "The wrapper approach works particularly well in an Enterprise Bean that uses Bean-managed persistence for two reasons: 1) Only one Driver class is loaded per Bean, and 2) specific connection details are handled outside the Bean. "

For my DBLog approach , is it ok to put it in wrapper class? Because I am not creating any entity bean...
0
 
LVL 11

Expert Comment

by:petiex
ID: 37739237
I've honestly never had to worry about the implementation of a Connection Pool, because the app servers I have used, Websphere and Glassfish, both have a built in connection pool facility which you configure to point to your data sources using their respective web-based admin consoles. You just need to provide it the login credentials, db name and server address, port number, any proprietary db access jar files, and a jndi name to look it up by. Then in your application, you access the data source using a JNDI lookup.

You might look into whether the app server or web server you are using has such a facility built-in. The odds are pretty good that it does.

If you only have one data source, it might actually be simpler to roll your own Connection Pool as described in that tutorial you linked to. As far as I can tell, there is no requirement to be using entity beans; they just point out that it's particularly well suited for them.
0
 

Author Closing Comment

by:weblogicme
ID: 37797779
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question