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);
}
}
Aggregations