Search in sources :

Example 6 with UserDBConnections

use of com.yahoo.dba.perf.myperf.db.UserDBConnections in project mysql_perf_analyzer by yahoo.

the class WebAppUtil method getDBConnection.

public static DBConnectionWrapper getDBConnection(HttpServletRequest req, MyPerfContext ctx, DBInstanceInfo dbinfo) {
    try {
        //get session
        HttpSession sess = req.getSession();
        //get connection manager
        UserDBConnections conns = UserDBConnections.class.cast(sess.getAttribute("UserDBConnections"));
        //if none, we need add one
        if (conns == null) {
            conns = new UserDBConnections();
            conns.setFrameworkContext(ctx);
            conns.setConnectionIdleTimeout(ctx.getConnectionIdleTime());
            //we don't have validated user, too
            conns.setAppUser(WebAppUtil.findUserFromRequest(req));
            sess.setAttribute("UserDBConnections", conns);
        }
        conns.closeExpired(ctx.getConnectionIdleTime());
        //first try to get connection without cred, meaning from saved
        DBConnectionWrapper connWrapper = conns.checkoutConnection(dbinfo, null);
        if (connWrapper != null) {
            //we will waste one query to check connectivity to avoid annoying error message
            if (DBUtils.checkConnection(connWrapper.getConnection()))
                return connWrapper;
            closeDBConnection(req, connWrapper, true, false);
            connWrapper = null;
        }
        //now get credential
        AppUser appUser = null;
        DBCredential cred = null;
        appUser = AppUser.class.cast(req.getSession().getAttribute(AppUser.SESSION_ATTRIBUTE));
        if (appUser == null)
            throw new RuntimeException("No user found. Session might not be valid.");
        cred = WebAppUtil.findDBCredential(ctx, dbinfo.getDbGroupName(), appUser);
        if (cred == null || cred.getPassword() == null)
            throw new RuntimeException("No valid credential provided for DB " + dbinfo.getDbGroupName());
        if (dbinfo.isConnectionVerified() || !dbinfo.supportClusterQuery()) {
            connWrapper = conns.checkoutConnection(dbinfo, cred);
            if (!DBUtils.checkConnection(connWrapper.getConnection())) {
                closeDBConnection(req, connWrapper, true, false);
                connWrapper = conns.checkoutConnection(dbinfo, cred);
            }
        }
        if (connWrapper == null && dbinfo.supportClusterQuery())
            connWrapper = conns.checkoutConnection(ctx.getDbInfoManager().findGroup(dbinfo.getDbGroupName()), cred);
        if (connWrapper == null)
            throw new RuntimeException("failed to connect to target db (" + dbinfo + ")");
        return connWrapper;
    } catch (Throwable th) {
        if (th instanceof RuntimeException)
            throw RuntimeException.class.cast(th);
        throw new RuntimeException(th);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) UserDBConnections(com.yahoo.dba.perf.myperf.db.UserDBConnections) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential)

Aggregations

UserDBConnections (com.yahoo.dba.perf.myperf.db.UserDBConnections)6 HttpSession (javax.servlet.http.HttpSession)3 AppUser (com.yahoo.dba.perf.myperf.common.AppUser)1 ConfigBlock (com.yahoo.dba.perf.myperf.common.ConfigBlock)1 DBCredential (com.yahoo.dba.perf.myperf.common.DBCredential)1 DBGroupInfo (com.yahoo.dba.perf.myperf.common.DBGroupInfo)1 DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)1 MyPerfContext (com.yahoo.dba.perf.myperf.common.MyPerfContext)1 DBConnectionWrapper (com.yahoo.dba.perf.myperf.db.DBConnectionWrapper)1 File (java.io.File)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 RedirectView (org.springframework.web.servlet.view.RedirectView)1