aspose file tools*
The moose likes Servlets and the fly likes Connection Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Connection Object " Watch "Connection Object " New topic
Author

Connection Object

Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
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

Joined: Sep 11, 2002
Posts: 167
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.


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Glenny Dsilva
Ranch Hand

Joined: May 09, 2005
Posts: 42
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

Joined: Sep 11, 2002
Posts: 167
The code looks good to me. Can you verify that you are getting the correct DB name from the session?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Object