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

Servlet --> Connection

Philip Pross
Ranch Hand

Joined: Jan 17, 2001
Posts: 76
Hello to all, I',m presently working on an application involving servlets and odbc driver which is on a TomCat enviroment. I have 18 servlets that are called depending on what is needed to do, as for the connection, there is only one made .. once the user logs in. For the rest of the connection, I'm reusing the method from MyLogin.java (servlet) ...... ....
public void init(ServletConfig config) throws ServletException
{
super.init(config);
c =MyLogin.getConnection();
}
This process works fine for one user, but when there are 2 or more on line ..... I'm getting error messages!!
Is this the way to go or should I create a new connection for each servlet
Mayer Salzer
Greenhorn

Joined: Apr 01, 2002
Posts: 20
Hi,
Your problems may be due to the fact that you're using the same connection for multiple users.
I'm assuming that the variable 'c' [as in --> c =MyLogin.getConnection(); ] is declared at the class level. You didn't say how "MyLogin.getConnection()" creates connections. However, even if it is thread safe (by creating a new connection each time or obtaining it from a connection pool) you're still storing it in the member variable 'c'. This means that every call to the same servlet will use the same instance of the connection (unless your servlets implement the SingleThreadModel interface).
Obtaining a new connection for each of your 18 servlets won't help you either, since 2 users could be hitting the same servlet at once.
Instead, you should probably obtain your connections in the methods you actually use them. For efficiency, this should be done through connection pooling. Again, you didn't say how "MyLogin.getConnection()" creates/obtains the connections, but even if it is done via pooling, you should still return the connection when you're done with it (usually at the end of the method where you used it).
Good luck.


Sun Certified Programmer for Java 2 Platform
Philip Pross
Ranch Hand

Joined: Jan 17, 2001
Posts: 76
Thanks for fast reply Mayer, as for the connection here is part of my code.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import java.util.Date;

public class MyLogin extends HttpServlet
{
static Connection c = null;
//********************************************************************************************
public String getServletInfo()
{
return "Bienvenue Servlet: MyLogin.java";
}

public void init(ServletConfig config) throws ServletException
{
super.init(config);

String url = "jdbc dbc b";
String nom="JAVA";
String pw="JAVA";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
c = DriverManager.getConnection(url, nom, pw);
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage() );
}
catch (Exception ee)
{
System.out.println(ee.getMessage() );
}
}
//********************************************************************************************
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
.......
.......

}
//********************************************************************************************
public void destroy()
{
try
{
c.close();
c = null;
}
catch(Exception e)
{
System.out.println("erreur de statement " + e.getMessage());
}
}
public static Connection getConnection()
{
return c;
}

}
thanks again
Rowan Brownlee
Ranch Hand

Joined: Aug 07, 2000
Posts: 97
Hi Philip,
I found some useful connection pooling programs in Marty Hall's "Core Servlets and java server pages"
rowan
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
The problem is that your servlet has a static Connection object. Because the servlet is running in a multi-threading environment, things go wrong with the Conncection object when multiple users visit your servlet simultaneously. You may try to put your connection in the doPost()/doGet() method. And get a new conncection instance for every request. Make sure that you close() at the end of the method. If this becomes an overhead, try to use a database connection pool.
Philip Pross
Ranch Hand

Joined: Jan 17, 2001
Posts: 76
Thanks to all for the input, I finally understood with a connection pool it allows multi-user and at the same time it should be done this way, thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet --> Connection