This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet --> Connection

 
Philip Pross
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Philip Pross
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Philip,
I found some useful connection pooling programs in Marty Hall's "Core Servlets and java server pages"
rowan
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic