Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection Object

 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am maintaining a project where there is one domain i.e. connecting to 2 databases.

The system gets connected to the database according to database parameter then the database name gets stored in the session variable to access in other pages.

my problem is that when i connect to the first database i get data of the second database it is only happing in one servlet.Rest of the servlets are working fine.


I think its due to this line of code where an instance of a particular class is created con is the connection instance i.e. passed to the constructor code is below.

InfoHelper ih = new InfoHelper(con,empcode);

There are some methods in the InfoHelper where i get data from and those methods give wrong data of other database.

Can the code above create a problem or it can be a session variable problem

should we put it in a syncronized block

synchronized(this)
{
ih = new InfoHelper(con,empcode,dbname);
}

can anyone give me a proper solution.
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think synchronization will help. Somehow the wrong connection is being placed in the session. Please check the code that creates the connection and subsequently places it into the session.
 
Glenny Dsilva
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are not storing the connection object in the session
we are only storing the database name in the session.


in the calling program we write

dbname = s.getAttribute("DB").toString(); // database in session
Connection con = ConnectionClass.getConnection(<databasename in session variable> ;

the method in the ConnectionClass is below

public static Connection getConnection(String dbname)
{
Connection con=null;

try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost/"+dbname,"username","password");
} // try closes

catch(ClassNotFoundException e)
{
Log.printStackTrace(e);
} // catch closes

catch(SQLException e)
{
System.out.println(e.toString());
Log.printStackTrace(e);
} // catch closes
return con;
}
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code looks good to me. Can you verify that you are getting the correct DB name from the session?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic