简体   繁体   中英

Connecting to Oracle database from a Servlet takes a long time

I am developing a dashboard application where i need to poll the database continuously (every 30s) for checking whether the database is up and running. In order to find out if the database is working, i am making a simple query to retrieve just 1 row from a table.

Everything works fine, but the problem is the servlet takes an unusually long time to establish a connection to the database and retrieve a row. It takes almost 15 seconds for this process. I copied the same code into a java file to find out whether it still takes that much time. But, as i suspected, it took just 1 or 2 seconds for the java program to run. It seems there is something wrong with the way i am approaching this whole thing.

I wrote the database connectivity code in doGet() function since i need to poll the database repeatedly. Is this the mistake i am committing? Should i write database connectivity code in init() and just write the query in doGet() function?

Any help will be appreciated. Thanks

Here is a part of code that might help you understand my problem:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Send Request to DB CAT
LinkedHashMap<String,String> hm=new LinkedHashMap<String,String>();
   try
   {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    //CONNECT TO DB
    String serverName = "*****.1dc.com"; // * is just to hide the real database name
    int port = 1535;
    String user = "mbank";
    String password = "****";
    String SID = "mbkwqa";
    String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
    Connection conndb = DriverManager.getConnection(URL, user, password);
    String SQL = "select CITY from ADDRESS where rownum<=1";
    Statement stat = conndb.createStatement();
    ResultSet rs = stat.executeQuery(SQL);
    while (rs.next()) 
    {
        System.out.println(rs.getString(1));
    }
    stat.close();
    conndb.close(); 
    hm.put("Server1","OK");
    }
      catch(Exception e)
    {
        hm.put("Server1","Failed");         
    }   

you are connecting to database from doGet function and this is very bad practice. What would happen if you have 100, 1000, 10000 requests to your servlet?

You need to review your design.

Please create Connection pool to your Oracle data Source, put it in Servlet ApplicationContext, Create Dao Classes and call your Dao Classes from doGet Servlet.

I believe these tutorials will help:

http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html

this is not a good practice build a connection to DB for every time of request coming. your should create connection pool, it will saving much time of connection. have look at tomcat conntion pool or apache DBCP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM