简体   繁体   中英

Throwing an exception into the Servlet

I have a Servlet called ProblemServlet . I have a database class called ProblemTable . Now, the below is a single method from that ProblemTable class.

package DB;

import Controller.Common;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ProblemsTable extends DBMaster
{
    public void deleteProblem(int idProblem)
    {
        checkConnection();
        PreparedStatement ps = null;
        String result="";

        try
        {
            con.setAutoCommit(false);
            String sql = "DELETE FROM Problem WHERE idProblem=?";
            ps = con.prepareStatement(sql);

            ps.setInt(1,idProblem);

            int i = ps.executeUpdate();
            con.commit();

            if(i>0)
            {
                result="Deleted Succesfully";
            }
            else
            {
                result="Delete Failed";
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            result = "Delete Failed. Rolled back";

            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
                result = "Delete failed. Rollback failed";
            }
        }
    }
}

As you can see, I am capturubg the exception inside this class. However I would like to catch these exceptions inside the ProblemServlet , because I can then display the error as a alert()

Now my problem is this.

If I throw the exception, then I will not be able to call con.rollback() or ps.close because the servlet has no access to ps or con and the access cannot be given too.

How can I throw the exception, handling these issues as well?

This is quite simple, you can rethrow the exception after you'v done with rollback, or throw another exception with new:

//1

catch(SQLException e)
    {
        e.printStackTrace();
        result = "Delete Failed. Rolled back";

        try {
            con.rollback();
        } catch (SQLException ex) {
            ex.printStackTrace();
            result = "Delete failed. Rollback failed";
        }
        throw e;
    }

//2

catch(SQLException e)
    {
        e.printStackTrace();
        result = "Delete Failed. Rolled back";

        try {
            con.rollback();
        } catch (SQLException ex) {
            ex.printStackTrace();
            result = "Delete failed. Rollback failed";
        }
        throw new Exception(...);
    }

Some ways i can think of:

  • Inject the parent in your class and expose setter so you could have access to connection and prepared statement. I would not recommend this worst way!
  • You close the connection or rollback.. and statement and then rethrow the exception like:

      catch(SQLException ...) { //close statement //rollback changes throw .... } 

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