Pregunta

I made a Java Webapp with Spring and Hibernate where I have to take a lot of data from the database, but I have a problem, because the app is too slowly and I need to make it faster. I think that the higher problem is the amount of conexions that I do in each jsp file, and I don't know how to solve it. That conexions I think are all neccesary, but maybe I can modify some part of code to make it faster. I pass the data to the jsp via Hibernate and Spring in the model, but then in the jsp I have to make a connection with the database to take some more information that are in another class. For example, I have some players, and in the player I have the ID of their team, so I have to connect in the jsp to take the name of the team, because I only know the ID. I show you how I connect the app with the database:

try {
    Class.forName("org.gjt.mm.mysql.Driver");
    Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "user", "pwd");
    if (!conexion.isClosed()) {
        // La consulta
        Statement st = conexion.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM entity");

        while (rs.next()){
            aaa = rs.getObject("aaa").toString();
        }

        // cierre de la conexion
        conexion.close();
    }
    else 
        // Error en la conexion
        out.println("fallo");
}
catch (Exception e) {
    // Error en algun momento.
    out.println("Excepcion "+e);
    e.printStackTrace();
}

And I do this same a lot of times in the same jsp, even 10-15 times in the biggers jsp. Can I open the conexion in the begin of the jsp and close the conexion in the end and put all the jsp code and html inside the conexion with the database to make only 1 connection?

And another doubt, in the same conection, if I have to do 2 querys, I do like this:

        Statement st = conexion.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM entity");

        while (rs.next()){
            aaa = rs.getObject("aaa").toString();
        }

        st = conexion.createStatement();
        rs = st.executeQuery("select * from entity2");

        while (rs.next()){
            bbb = rs.getObject("bbb").toString();
        }

So, I create a new Statement for the query, is it neccesary? Or is enough if I create the Statement in the first query? And is this important in the speed of the page?

Does someone know what could I do to make my app faster?

Thanks!

¿Fue útil?

Solución

One Statement st = conexion.createStatement(); is sufficient. Don't keep recreating the statement. You only have a limited amount of times you can call createStatement. If you need a nested query, make two statements, st and st2, but do not keep calling createStatement for the same variable.

Also, if you are going to use connections in scriptlets, you should at the very least make a class to put the actual connecting part in, i.e. Class.forName("org.gjt.mm.mysql.Driver"); Connection conexion = DriverManager.getConnection(....); and then just call that in the JSP. That way you don't have messy connection code in every JSP and when you have to change the server IP or username or something you won't have to go change a slew of JSPs.

And don't open the connection 10 times in one JSP. You wouldn't even want to do that in a Servlet. You would want to create a member variable that is of type Connection, open it, and keep it open and until the end.

If you are going to use scriptlets, your code should be at least this clean:

<%
//call a static method in a class you create to get the connection
Connection connection = Appname.dbclass.getConnection();
if(connection==null)
{
   out.print("error connecting");
   return;
}
Statement st = connection.createStatement();
ResultSet rs = null;
...
//do all your stuff
...
if(rs!=null)
{
   try
   {
     rs.close();
   }
   catch(Exception ex){}
}
if(st!=null)
{
   try
   {
     st.close();
   }
   catch(Exception ex){}
}
if(connection!=null)
{
   try
   {
     connection.close();
   }
   catch(Exception ex){}
}
%>

Also, instead of doing "select * from table" followed by something like:

while (rs.next()){
  aaa = rs.getObject("aaa").toString();
 }

use a WHERE CLAUSE in your SQL to limit the number of rows returned to ONE, and also limit the fields you pull to the ones you need ("select aaa from table WHERE id=1") and then an if-statement:

  if (rs.next()){
   aaa = rs.getObject("aaa").toString();
  }

With your present code, each time you go through the loop again, aaa is being overwritten with a new value. Its wasting a ton of processing time either doing nothing or ensuring that the result is completely wrong.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top