remm        2005/03/08 15:03:01

  Modified:    catalina/src/share/org/apache/catalina/realm JDBCRealm.java
  Log:
  - Add back the reconnection logic to JDBC realm, which was removed for wrong 
reasons. From what I saw, it causes major issues with MySQL otherwise
    (obviously, another solution is to use the better data source realm).
  
  Revision  Changes    Path
  1.14      +100 -59   
jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/realm/JDBCRealm.java
  
  Index: JDBCRealm.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/realm/JDBCRealm.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- JDBCRealm.java    23 Feb 2005 19:27:56 -0000      1.13
  +++ JDBCRealm.java    8 Mar 2005 23:03:01 -0000       1.14
  @@ -521,40 +521,60 @@
           PreparedStatement stmt = null;
           ResultSet rs = null;
   
  -        try {
  -            stmt = credentials(dbConnection, username);
  -            rs = stmt.executeQuery();
  -
  -            if (rs.next()) {
  -                dbCredentials = rs.getString(1);
  -            }
  -            rs.close();
  -            rs = null;
  -            if (dbCredentials == null) {
  -                return (null);
  -            }
  -
  -            dbCredentials = dbCredentials.trim();
  -            return dbCredentials;
  -            
  -        } catch(SQLException e){
  -            
containerLog.error(sm.getString("jdbcRealm.getPassword.exception",
  -                                            username),
  -                               e);
  -        } finally {
  -            if (rs!=null) {
  +        // Number of tries is the numebr of attempts to connect to the 
database
  +        // during this login attempt (if we need to open the database)
  +        // This needs rewritten wuth better pooling support, the existing 
code
  +        // needs signature changes since the Prepared statements needs cached
  +        // with the connections.
  +        // The code below will try twice if there is a SQLException so the
  +        // connection may try to be opened again. On normal conditions 
(including
  +        // invalid login - the above is only used once.
  +        int numberOfTries = 2;
  +        while (numberOfTries>0) {
  +            try {
  +                
  +                // Ensure that we have an open database connection
  +                open();
  +                
                   try {
  +                    stmt = credentials(dbConnection, username);
  +                    rs = stmt.executeQuery();
  +                    
  +                    if (rs.next()) {
  +                        dbCredentials = rs.getString(1);
  +                    }
                       rs.close();
  -                } catch(SQLException e) {
  -                    
containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet"));
  +                    rs = null;
  +                    if (dbCredentials == null) {
  +                        return (null);
  +                    }
  +                    
  +                    dbCredentials = dbCredentials.trim();
  +                    return dbCredentials;
  +                    
  +                } finally {
  +                    if (rs!=null) {
  +                        try {
  +                            rs.close();
  +                        } catch(SQLException e) {
  +                            
containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet"));
  +                        }
  +                    }
  +                    dbConnection.commit();
                   }
  -            }
  -            try {
  -                dbConnection.commit();
  +                
               } catch (SQLException e) {
  -                
containerLog.warn(sm.getString("jdbcRealm.getPassword.exception",
  -                                               username));
  +                
  +                // Log the problem for posterity
  +                containerLog.error(sm.getString("jdbcRealm.exception"), e);
  +                
  +                // Close the connection so that it gets reopened next time
  +                if (dbConnection != null)
  +                    close(dbConnection);
  +                
               }
  +            
  +            numberOfTries--;
           }
           
           return (null);
  @@ -582,41 +602,62 @@
           PreparedStatement stmt = null;
           ResultSet rs = null;
   
  -        try {
  -            // Accumulate the user's roles
  -            ArrayList roleList = new ArrayList();
  -            stmt = roles(dbConnection, username);
  -            rs = stmt.executeQuery();
  -            while (rs.next()) {
  -                String role = rs.getString(1);
  -                if (null!=role) {
  -                    roleList.add(role.trim());
  -                }
  -            }
  -            rs.close();
  -            rs = null;
  -            
  -            return (roleList);
  -            
  -        } catch(SQLException e){
  -            containerLog.error(sm.getString("jdbcRealm.getRoles.exception",
  -                                            username));
  -        } finally {
  -            if (rs!=null) {
  +        // Number of tries is the numebr of attempts to connect to the 
database
  +        // during this login attempt (if we need to open the database)
  +        // This needs rewritten wuth better pooling support, the existing 
code
  +        // needs signature changes since the Prepared statements needs cached
  +        // with the connections.
  +        // The code below will try twice if there is a SQLException so the
  +        // connection may try to be opened again. On normal conditions 
(including
  +        // invalid login - the above is only used once.
  +        int numberOfTries = 2;
  +        while (numberOfTries>0) {
  +            try {
  +                
  +                // Ensure that we have an open database connection
  +                open();
  +                
                   try {
  +                    // Accumulate the user's roles
  +                    ArrayList roleList = new ArrayList();
  +                    stmt = roles(dbConnection, username);
  +                    rs = stmt.executeQuery();
  +                    while (rs.next()) {
  +                        String role = rs.getString(1);
  +                        if (null!=role) {
  +                            roleList.add(role.trim());
  +                        }
  +                    }
                       rs.close();
  -                } catch(SQLException e) {
  -                    
containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet"));
  +                    rs = null;
  +                    
  +                    return (roleList);
  +                    
  +                } finally {
  +                    if (rs!=null) {
  +                        try {
  +                            rs.close();
  +                        } catch(SQLException e) {
  +                            
containerLog.warn(sm.getString("jdbcRealm.abnormalCloseResultSet"));
  +                        }
  +                    }
  +                    dbConnection.commit();
                   }
  -            }
  -            try {
  -                dbConnection.commit();
  +                
               } catch (SQLException e) {
  -                
containerLog.warn(sm.getString("jdbcRealm.getRoles.exception",
  -                                               username));
  +                
  +                // Log the problem for posterity
  +                containerLog.error(sm.getString("jdbcRealm.exception"), e);
  +                
  +                // Close the connection so that it gets reopened next time
  +                if (dbConnection != null)
  +                    close(dbConnection);
  +                
               }
  +            
  +            numberOfTries--;
           }
  -
  +        
           return (null);
           
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to