⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlutilprofiler.java

📁 Jodd是一个开源的公用Java基础类库
💻 JAVA
字号:
package jodd.db.profile;

import java.sql.ResultSet;
import java.sql.SQLException;

import jodd.db.SqlUtil;
import jodd.db.pool.ConnectionPool;

/**
 * Profiles database access by measuring <code>executeQuery</code> and
 * <code>executeUpdate</code> execution times. After measuring, a callback
 * method in <code>SqlProfilerHandler</code> is called with profile data.
 * <p>
 *
 * Usage is quite simple: instead of <code>SqlUtil</code> use
 * <code>SqlUtilProfiler</code>. Before first usage, usually during
 * initialization, assign handler field to an instance of the
 * <code>SqlProfilerHandler</code>.
 *
 * <p>Note: java System.currentTimeMillis() is not very precise, so
 * as query execution is longer, the duration is more precise, and the
 * error is lower.
 *
 * @see jodd.db.SqlUtil
 */
public class SqlUtilProfiler extends SqlUtil {

	/**
	 * Holds current profile handler. Must be set before first usage.
	 */
	public static SqlProfilerHandler handler = null;

	/**
	 * Overloaded constructor.
	 *
	 * @param cp
	 */
	public SqlUtilProfiler(ConnectionPool cp) {
		super(cp);
	}

	/**
	 * Executes query and measures time.
	 *
	 * @return sql query result
	 * @exception SQLException
	 */
	public ResultSet executeQuery() throws SQLException {
		if (handler == null) {
			return super.executeQuery();
		}
		SqlProfileData spdata = new SqlProfileData();
		spdata.setSqlQuery(this.toString());
		long start = System.currentTimeMillis();
		ResultSet rs = null;
		try {
			spdata.setQueryTime(System.currentTimeMillis());
			rs = super.executeQuery();
			spdata.setDuration(System.currentTimeMillis() - start);
		} catch (SQLException sex) {
			spdata.setDuration(System.currentTimeMillis() - start);
			throw sex;
		} finally {
			handler.onExecuteQuery(spdata);
		}
		return rs;
	}

	/**
	 * Executes update and measures time.
	 *
	 * @return sql update result
	 * @exception SQLException
	 */
	public int executeUpdate() throws SQLException {
		if (handler == null) {
			return super.executeUpdate();
		}
		SqlProfileData spdata = new SqlProfileData();
		spdata.setSqlQuery(this.toString());
		long start = System.currentTimeMillis();
		int r = 0;
		try {
			spdata.setQueryTime(System.currentTimeMillis());
			r = super.executeUpdate();
			spdata.setDuration(System.currentTimeMillis() - start);
		} catch (SQLException sex) {
			spdata.setDuration(System.currentTimeMillis() - start);
			throw sex;
		} finally {
			handler.onExecuteUpdate(spdata);
		}
		return r;
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -