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

📄 sessionassociationinterceptor.java

📁 mysql5.0 JDBC 驱动 放在glassfish或者tomcat的lib文件夹下就可以了
💻 JAVA
字号:
package com.mysql.jdbc.interceptors;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetInternalMethods;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.StatementInterceptor;

public class SessionAssociationInterceptor implements StatementInterceptor {

	protected String currentSessionKey;
	protected static ThreadLocal sessionLocal = new ThreadLocal();
	
	public static final void setSessionKey(String key) {
		sessionLocal.set(key);
	}
	
	public static final void resetSessionKey() {
		sessionLocal.set(null);
	}
	
	public static final String getSessionKey() {
		return (String)sessionLocal.get();
	}
	
	public boolean executeTopLevelOnly() {
		return true;
	}

	public void init(Connection conn, Properties props) throws SQLException {
		// TODO Auto-generated method stub

	}

	public ResultSetInternalMethods postProcess(String sql,
			Statement interceptedStatement,
			ResultSetInternalMethods originalResultSet, Connection connection)
			throws SQLException {
		return null;
	}

	public ResultSetInternalMethods preProcess(String sql,
			Statement interceptedStatement, Connection connection)
			throws SQLException {
		String key = getSessionKey();
		
		if (key != null && !key.equals(this.currentSessionKey)) {
			PreparedStatement pstmt = connection.clientPrepareStatement("SET @mysql_proxy_session=?");
			
			try {
				pstmt.setString(1, key);
				pstmt.execute();
			} finally {
				pstmt.close();
			}
			
			this.currentSessionKey = key;
		}
		
		return null;
	}

	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}

⌨️ 快捷键说明

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