Search in sources :

Example 6 with DBConnectionWrapper

use of com.yahoo.dba.perf.myperf.db.DBConnectionWrapper 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

DBConnectionWrapper (com.yahoo.dba.perf.myperf.db.DBConnectionWrapper)6 DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)4 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)3 Statement (java.sql.Statement)3 HashMap (java.util.HashMap)3 DBCredential (com.yahoo.dba.perf.myperf.common.DBCredential)2 Metric (com.yahoo.dba.perf.myperf.common.Metric)2 MetricsGroup (com.yahoo.dba.perf.myperf.common.MetricsGroup)2 QueryParameters (com.yahoo.dba.perf.myperf.common.QueryParameters)2 ResultList (com.yahoo.dba.perf.myperf.common.ResultList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 AppUser (com.yahoo.dba.perf.myperf.common.AppUser)1 ColumnDescriptor (com.yahoo.dba.perf.myperf.common.ColumnDescriptor)1 ConfigBlock (com.yahoo.dba.perf.myperf.common.ConfigBlock)1 ResultRow (com.yahoo.dba.perf.myperf.common.ResultRow)1 UserDefinedMetrics (com.yahoo.dba.perf.myperf.common.UserDefinedMetrics)1 UserDBConnections (com.yahoo.dba.perf.myperf.db.UserDBConnections)1