• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet --> Connection

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic