use of com.yahoo.dba.perf.myperf.common.AppUser 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);
}
}
use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.
the class MetaDB method changePrivilege.
/**
* Update user privilege
* @param user
* @param newPrivilege
* @return
*/
public boolean changePrivilege(String user, int newPrivilege) {
if (user == null)
return false;
String sql2 = "update " + APPUSER_TABLENAME + " set USER_PRIVILEGE=? where USERNAME=?";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
//first, check if we have record
AppUser appUser = this.retrieveUserInfo(conn, user);
if (appUser == null)
return false;
appUser.setUserprivilege(newPrivilege);
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, appUser.getUserprivilege());
pstmt.setString(2, appUser.getName().toLowerCase());
pstmt.execute();
conn.commit();
return true;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Exception", ex);
if (conn != null)
try {
conn.rollback();
} catch (Exception iex) {
}
throw new RuntimeException(ex);
} finally {
DBUtils.close(pstmt);
DBUtils.close(conn);
}
}
use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.
the class MetaDB method changeEmail.
/**
* Update user email address
* @param user
* @param newEmail
* @return
*/
public boolean changeEmail(String user, String newEmail) {
if (user == null)
return false;
String sql2 = "update " + APPUSER_TABLENAME + " set EMAIL=? where USERNAME=?";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
//first, check if we have record
AppUser appUser = this.retrieveUserInfo(conn, user);
if (appUser == null)
return false;
pstmt = conn.prepareStatement(sql2);
pstmt.setString(1, newEmail);
pstmt.setString(2, appUser.getName().toLowerCase());
pstmt.execute();
conn.commit();
return true;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Exception", ex);
if (conn != null)
try {
conn.rollback();
} catch (Exception iex) {
}
throw new RuntimeException(ex);
} finally {
DBUtils.close(pstmt);
DBUtils.close(conn);
}
}
use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.
the class MetaDB method confirmUser.
public boolean confirmUser(String user, boolean confirmed) {
if (user == null)
return false;
String sql2 = "update " + APPUSER_TABLENAME + " set VERIFIED=? where USERNAME=?";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
//first, check if we have record
AppUser appUser = this.retrieveUserInfo(conn, user);
if (appUser == null)
return false;
pstmt = conn.prepareStatement(sql2);
pstmt.setInt(1, confirmed ? 1 : 0);
pstmt.setString(2, appUser.getName().toLowerCase());
pstmt.execute();
conn.commit();
return true;
} catch (Exception ex) {
logger.log(Level.SEVERE, "Exception", ex);
if (conn != null)
try {
conn.rollback();
} catch (Exception iex) {
}
throw new RuntimeException(ex);
} finally {
DBUtils.close(pstmt);
DBUtils.close(conn);
}
}
use of com.yahoo.dba.perf.myperf.common.AppUser in project mysql_perf_analyzer by yahoo.
the class MetaDB method retrieveUserInfo.
private AppUser retrieveUserInfo(Connection conn, String username) {
String sql = "select * from " + APPUSER_TABLENAME + " where username=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username.toLowerCase());
rs = pstmt.executeQuery();
if (rs != null && rs.next()) {
AppUser user = new AppUser();
user.setName(rs.getString("USERNAME"));
user.setMd5Hash(rs.getString("MD5HASH"));
user.setUserprivilege(rs.getShort("USER_PRIVILEGE"));
user.setEmail(rs.getString("EMAIL"));
user.setVerified("1".equals(rs.getString("VERIFIED")));
return user;
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Exception", ex);
} finally {
DBUtils.close(rs);
DBUtils.close(pstmt);
}
return null;
}
Aggregations