Code example for Connection

Methods: close, commit, getAutoCommit, prepareStatement

0
        if (dbConnection == null)
            return; 
 
        // Commit if not auto committed 
        try { 
            if (!dbConnection.getAutoCommit()) {
                dbConnection.commit();
            }             
        } catch (SQLException e) {
            containerLog.error("Exception committing connection before closing:", e);
        } 
 
        // Close this database connection, and log any errors 
        try { 
            dbConnection.close();
        } catch (SQLException e) {
            containerLog.error(sm.getString("dataSourceRealm.close"), e); // Just log it here
        } 
 
    } 
 
    /** 
     * Open the specified database connection. 
     * 
     * @return Connection to the database 
     */ 
    protected Connection open() {
 
        try { 
            Context context = null;
            if (localDataSource) {
                context = ContextBindings.getClassLoader();
                context = (Context) context.lookup("comp/env");
            } else { 
                StandardServer server = 
                    (StandardServer) ServerFactory.getServer(); 
                context = server.getGlobalNamingContext();
            } 
            DataSource dataSource = (DataSource)context.lookup(dataSourceName);
	    return dataSource.getConnection();
        } catch (Exception e) {
            // Log the problem for posterity 
            containerLog.error(sm.getString("dataSourceRealm.exception"), e);
        }   
        return null; 
    } 
 
    /** 
     * Return a short name for this Realm implementation. 
     */ 
    protected String getName() {
 
        return (name);
 
    } 
 
    /** 
     * Return the password associated with the given principal's user name. 
     */ 
    protected String getPassword(String username) {
 
        Connection dbConnection = null;
 
        // Ensure that we have an open database connection 
        dbConnection = open();
        if (dbConnection == null) {
            return null; 
        } 
 
        try { 
        	return getPassword(dbConnection, username);        	
        } finally { 
            close(dbConnection);
        } 
    } 
     
    /** 
     * Return the password associated with the given principal's user name. 
     * @param dbConnection The database connection to be used 
     * @param username Username for which password should be retrieved 
     */ 
    protected String getPassword(Connection dbConnection, 
								 String username) {
 
        ResultSet rs = null;
        PreparedStatement stmt = null;
        String dbCredentials = null;
 
        try { 
            stmt = credentials(dbConnection, username);
            rs = stmt.executeQuery();
            if (rs.next()) {
                dbCredentials = rs.getString(1);
            } 
 
            return (dbCredentials != null) ? dbCredentials.trim() : null;
             
        } catch(SQLException e) {
            containerLog.error( 
                    sm.getString("dataSourceRealm.getPassword.exception",
                                 username));
        } finally { 
        	try { 
	            if (rs != null) {
	                rs.close();
	            } 
	            if (stmt != null) {
	                stmt.close();
	            } 
        	} catch (SQLException e) {
                    containerLog.error( 
                        sm.getString("dataSourceRealm.getPassword.exception",
        		             username));
        		 
        	} 
        } 
         
        return null; 
    } 
 
 
    /** 
     * Return the Principal associated with the given user name. 
     */ 
    protected Principal getPrincipal(String username) {
    	Connection dbConnection = open();
        if (dbConnection == null) {
            return new GenericPrincipal(this,username, null, null);
        } 
        try { 
        	return (new GenericPrincipal(this,
        			username,
					getPassword(dbConnection, username),
					getRoles(dbConnection, username)));
        } finally { 
        	close(dbConnection);
        } 
 
    } 
 
    /** 
     * Return the roles associated with the given user name. 
     * @param username Username for which roles should be retrieved 
     */ 
    protected ArrayList getRoles(String username) {
 
        Connection dbConnection = null;
 
        // Ensure that we have an open database connection 
        dbConnection = open();
        if (dbConnection == null) {
            return null; 
        } 
 
        try { 
            return getRoles(dbConnection, username);
        } finally { 
        	close(dbConnection);
        } 
    } 
     
    /** 
     * Return the roles associated with the given user name 
     * @param dbConnection The database connection to be used 
     * @param username Username for which roles should be retrieved 
     */ 
    protected ArrayList getRoles(Connection dbConnection,
                                     String username) {
    	 
        ResultSet rs = null;
        PreparedStatement stmt = null;
        ArrayList list = null;
    	 
        try { 
    		stmt = roles(dbConnection, username);
    		rs = stmt.executeQuery();
    		list = new ArrayList();
    		 
    		while (rs.next()) {
    			String role = rs.getString(1);
    			if (role != null) {
    				list.add(role.trim());
    			} 
    		} 
    		return list;
    	} catch(SQLException e) {
            containerLog.error( 
                sm.getString("dataSourceRealm.getRoles.exception", username));
        } 
    	finally { 
        	try { 
	            if (rs != null) {
	                rs.close();
	            } 
	            if (stmt != null) {
	                stmt.close();
	            } 
        	} catch (SQLException e) {
                    containerLog.error( 
                        sm.getString("dataSourceRealm.getRoles.exception",
                                     username));
        	} 
        } 
    	 
    	return null; 
    } 
 
    /** 
     * Return a PreparedStatement configured to perform the SELECT required 
     * to retrieve user credentials for the specified username. 
     * 
     * @param dbConnection The database connection to be used 
     * @param username Username for which credentials should be retrieved 
     * 
     * @exception SQLException if a database error occurs 
     */ 
    private PreparedStatement credentials(Connection dbConnection,
                                            String username)
        throws SQLException { 
 
        PreparedStatement credentials =
            dbConnection.prepareStatement(preparedCredentials);
 
        credentials.setString(1, username);
        return (credentials);