interceptor拦截器典型应用实例----数据稽核
Interceptor攔截器的使用小結,程序實例是典型的 “數據稽核”過程,即在對數據庫中的數據進行修改后會自動添加對應的日志記錄在數據庫中對應的日志表中,用于記錄對數據庫中關鍵數據的修改過程和追蹤,最重要的應用主要是日志記錄功能,將業務層的邏輯處理和數據訪問層的處理分離開來。功能類似于spring 中的aop功能。
Pojo類代碼:Tuser類
package com.inspur.po;
?
import java.util.HashSet;
import java.util.Set;
?
/**
?* TUser entity. @author MyEclipse Persistence Tools
?*/
?
public class TUser implements java.io.Serializable {
?
?????? // Fields
?
?????? private Integer id;
?????? private String name;
?????? private Integer age;
?????? private Set TAddresses = new HashSet(0);
?
?????? // Constructors
?
?????? /** default constructor */
?????? public TUser() {
?????? }
?
?????? /** full constructor */
?????? public TUser(String name, Integer age, Set TAddresses) {
????????????? this.name = name;
????????????? this.age = age;
????????????? this.TAddresses = TAddresses;
?????? }
?
?????? // Property accessors
?
?????? public Integer getId() {
????????????? return this.id;
?????? }
?
?????? public void setId(Integer id) {
????????????? this.id = id;
?????? }
?
?????? public String getName() {
????????????? return this.name;
?????? }
?
?????? public void setName(String name) {
????????????? this.name = name;
?????? }
?
?????? public Integer getAge() {
????????????? return this.age;
?????? }
?
?????? public void setAge(Integer age) {
????????????? this.age = age;
?????? }
?
?????? public Set getTAddresses() {
????????????? return this.TAddresses;
?????? }
?
?????? public void setTAddresses(Set TAddresses) {
????????????? this.TAddresses = TAddresses;
?????? }
?
}
Taddress類代碼:
package com.inspur.po;
?
/**
?* TAddress entity. @author MyEclipse Persistence Tools
?*/
?
public class TAddress implements java.io.Serializable {
?
??? // Fields
?
??? private Integer id;
??? private TUser TUser;
??? private String address;
??? private String zipcode;
??? private String street;
?
??? // Constructors
?
??? /** default constructor */
??? public TAddress() {
??? }
?
??? /** full constructor */
??? public TAddress(TUser TUser, String address, String zipcode, String street) {
?????? this.TUser = TUser;
?????? this.address = address;
?????? this.zipcode = zipcode;
?????? this.street = street;
??? }
?
??? // Property accessors
?
??? public Integer getId() {
?????? return this.id;
??? }
?
??? public void setId(Integer id) {
?????? this.id = id;
??? }
?
??? public TUser getTUser() {
?????? return this.TUser;
??? }
?
??? public void setTUser(TUser TUser) {
?????? this.TUser = TUser;
??? }
?
??? public String getAddress() {
?????? return this.address;
??? }
?
??? public void setAddress(String address) {
?????? this.address = address;
??? }
?
??? public String getZipcode() {
?????? return this.zipcode;
??? }
?
??? public void setZipcode(String zipcode) {
?????? this.zipcode = zipcode;
??? }
?
??? public String getStreet() {
?????? return this.street;
??? }
?
??? public void setStreet(String street) {
?????? this.street = street;
??? }
?
}
Tauditlog類代碼:
package com.inspur.po;
?
/**
?* TAuditlog entity. @author MyEclipse Persistence Tools
?*/
?
public class TAuditlog implements java.io.Serializable {
?
??? // Fields
?
??? private Integer id;
??? private String user;
??? private String action;
??? private String entityname;
??? private String commit;
??? private Integer actiontflag;
?
??? // Constructors
?
??? /** default constructor */
??? public TAuditlog() {
??? }
?
??? /** full constructor */
??? public TAuditlog(String user, String action, String entityname,
?????????? String commit, Integer actiontflag) {
?????? this.user = user;
?????? this.action = action;
?????? this.entityname = entityname;
?????? this.commit = commit;
?????? this.actiontflag = actiontflag;
??? }
?
??? // Property accessors
?
??? public Integer getId() {
?????? return this.id;
??? }
?
??? public void setId(Integer id) {
?????? this.id = id;
??? }
?
??? public String getUser() {
?????? return this.user;
??? }
?
??? public void setUser(String user) {
?????? this.user = user;
??? }
?
??? public String getAction() {
?????? return this.action;
??? }
?
??? public void setAction(String action) {
?????? this.action = action;
??? }
?
??? public String getEntityname() {
?????? return this.entityname;
??? }
?
??? public void setEntityname(String entityname) {
?????? this.entityname = entityname;
??? }
?
??? public String getCommit() {
?????? return this.commit;
??? }
?
??? public void setCommit(String commit) {
?????? this.commit = commit;
??? }
?
??? public Integer getActiontflag() {
?????? return this.actiontflag;
??? }
?
??? public void setActiontflag(Integer actiontflag) {
?????? this.actiontflag = actiontflag;
??? }
?
}
日志記錄類代碼:
package com.inspur.dao;
?
import java.sql.Connection;
import java.util.Iterator;
import java.util.Set;
?
import org.hibernate.CallbackException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
?
import com.inspur.po.TAuditlog;
import com.inspur.po.TUser;
?
?
public class AuditLogDaoImpl {
?????? public static void doLog(String action,String userId,Set modifyset,Connection connection){
????????????? Configuration config=new Configuration().configure();
????????????? SessionFactory sessionFactory=config.buildSessionFactory();
????????????? //重用interceptor中的session的jdbc Connection創建臨時session
????????????? Session session=sessionFactory.openSession(connection);
????????????? Iterator it=modifyset.iterator();
????????????? TAuditlog auditLog=new TAuditlog();
????????????? TUser user=null;
????????????? try{
???????????????????? while(it.hasNext()){
??????????????????????????? user=(TUser)it.next();
??????????????????????????? auditLog.setAction(action);
??????????????????????????? auditLog.setCommit(user.getName());
??????????????????????????? auditLog.setUser(userId);
??????????????????????????? auditLog.setActiontflag(user.getId());
??????????????????????????? //此次不用開啟transaction的原因就是臨時session重用了當前session的jdbc connection,而
??????????????????????????? //外圍已經開啟了當前session的 jdbc connection
??????????????????????????? session.save(auditLog);
??????????????????????????? //此次的臨時session是不關聯到攔截器的,所以不會觸發攔截器中的 postflush方法。
??????????????????????????? session.flush();
???????????????????????????
???????????????????? }
????????????? }catch(Exception e){
???????????????????? throw new CallbackException(e);
????????????? }finally{
???????????????????? try{
??????????????????????????? if(session!=null){
?????????????????????????????????? session.close();
??????????????????????????? }
???????????????????? }catch(Exception e1){
??????????????????????????? throw new CallbackException(e1);
???????????????????? }
????????????? }
?????????????
?????? }
?
}
攔截器類代碼:
package com.inspur.interceptor;
?
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
?
import org.hibernate.CallbackException;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.type.Type;
?
import com.inspur.dao.AuditLogDaoImpl;
import com.inspur.po.TUser;
?
public class MyInterceptor implements Interceptor{
?????? private Session session;
?????? private String userId;
?????? private Set insertSet=new HashSet();
?????? private Set updateSet=new HashSet();
??????
?????? public Session getSession() {
????????????? return session;
?????? }
?
?????? public void setSession(Session session) {
????????????? this.session = session;
?????? }
?
?????? public String getUserId() {
????????????? return userId;
?????? }
?
?????? public void setUserId(String userId) {
????????????? this.userId = userId;
?????? }
?
?????? public Set getInsertSet() {
????????????? return insertSet;
?????? }
?
?????? public void setInsertSet(Set insertSet) {
????????????? this.insertSet = insertSet;
?????? }
?
?????? public Set getUpdateSet() {
????????????? return updateSet;
?????? }
?
?????? public void setUpdateSet(Set updateSet) {
????????????? this.updateSet = updateSet;
?????? }
?
?????? @Override
?????? public void afterTransactionBegin(Transaction arg0) {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public void afterTransactionCompletion(Transaction arg0) {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public void beforeTransactionCompletion(Transaction arg0) {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
???????????????????? Object[] arg3, String[] arg4, Type[] arg5) {
????????????? // TODO Auto-generated method stub
????????????? return null;
?????? }
?
?????? @Override
?????? public Object getEntity(String arg0, Serializable arg1)
???????????????????? throws CallbackException {
????????????? // TODO Auto-generated method stub
????????????? return null;
?????? }
?
?????? @Override
?????? public String getEntityName(Object arg0) throws CallbackException {
????????????? // TODO Auto-generated method stub
????????????? return null;
?????? }
?
?????? @Override
?????? public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
???????????????????? throws CallbackException {
????????????? // TODO Auto-generated method stub
????????????? return null;
?????? }
?
?????? @Override
?????? public Boolean isTransient(Object arg0) {
????????????? // TODO Auto-generated method stub
????????????? return null;
?????? }
?
?????? @Override
?????? public void onCollectionRecreate(Object arg0, Serializable arg1)
???????????????????? throws CallbackException {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public void onCollectionRemove(Object arg0, Serializable arg1)
???????????????????? throws CallbackException {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public void onCollectionUpdate(Object arg0, Serializable arg1)
???????????????????? throws CallbackException {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
???????????????????? String[] arg3, Type[] arg4) throws CallbackException {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
?????? @Override
?????? public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
???????????????????? Object[]previousState , String[] propertyNames, Type[] types) throws CallbackException {
????????????? if(entity instanceof TUser){
???????????????????? updateSet.add(entity);
????????????? }
?????????????
????????????? return false;
?????? }
?
?????? @Override
?????? public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
???????????????????? String[] arg3, Type[] arg4) throws CallbackException {
????????????? // TODO Auto-generated method stub
????????????? return false;
?????? }
?
?????? @Override
?????? public String onPrepareStatement(String arg0) {
????????????? // TODO Auto-generated method stub
????????????? return arg0;
?????? }
?
?????? @Override
?????? public boolean onSave(Object entity, Serializable id, Object[] state,
???????????????????? String[] propertyNames, Type[] types) throws CallbackException {
????????????? if(entity instanceof TUser){
???????????????????? insertSet.add(entity);
????????????? }
?????????????
????????????? return false;
?????? }
?????? /*
?????? ?* 所有的sql都執行完成以后,即事物的提交完成以后再執行一次postflush方法,不與session的動作相關聯
?????? ?* 而是作為整個session生命周期結束的最終執行方法*/
?????? @Override
?????? public void postFlush(Iterator entity) throws CallbackException {
????????????? try{
???????????????????? if(insertSet.size()>0){
??????????????????????????? AuditLogDaoImpl.doLog("insert", userId, insertSet, session.connection());
??????????????????????????? System.out.println(insertSet.size());
???????????????????? }
???????????????????? if(updateSet.size()>0){
??????????????????????????? AuditLogDaoImpl.doLog("update", userId, updateSet, session.connection());
???????????????????? }
????????????? }catch(HibernateException e){
???????????????????? e.printStackTrace();
????????????????????
????????????? }
?????????????
?????? }
?
?????? @Override
?????? public void preFlush(Iterator arg0) throws CallbackException {
????????????? // TODO Auto-generated method stub
?????????????
?????? }
?
}
Test類代碼如下:
package com.inspur.test;
?
import java.util.List;
?
import org.hibernate.HibernateException;
/*
?* 程序的總體執行過程是:利用當前session的jdbc connection創建一個臨時的session用于將日志實體對象
?* 保存到數據庫中,在臨時session中不需要開啟和關閉事物,因為當前session的jdbc connection已經在
?* 之前開啟了,重用connection可以是一個事物只占用一個數據庫連接,提供數據訪問性能,在interceptor中
?* 是不能調用當前session的,因為interceptor中的所有方法都是由當前session負責調用的,如果在方法中調用
?* 當前session進行數據持久化操作,將導致session內部狀態混亂。*/
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
?
import com.inspur.interceptor.MyInterceptor;
import com.inspur.po.TUser;
?
import junit.framework.TestCase;
?
public class InterceptorTest extends TestCase {
??? Session session=null;
?
??? @Override
??? protected void setUp() throws Exception {
?????? Configuration config=new Configuration().configure();
?????? SessionFactory sessionFactory=config.buildSessionFactory();
?????? MyInterceptor interceptor_cus=new MyInterceptor();
?????? session=sessionFactory.openSession(interceptor_cus);
?????? interceptor_cus.setSession(session);
?????? interceptor_cus.setUserId("current user");
??????
??? }
?
??? @Override
??? protected void tearDown() throws Exception {
//???? session.close();
??????
??? }
??? public void testInterceptor(){
?????? TUser user=new TUser();
?????? user.setName("uuu_sky");
?????? Transaction tran=null;
?????? try{
?????????? tran=session.beginTransaction();
??????????
?????????? session.save(user);
?????????? //如果此處使用session.flush()方法那么實際上是調用了兩次dolog方法,在對應的日志數據庫中會
?????????? //插入兩條記錄,因為tran.commit()實際上包含了執行一次session.flush方法
??????????
//???????? session.flush();
?????????? tran.commit();
??????????
?????? }catch(HibernateException e){
?????????? e.printStackTrace();
?????????? tran.rollback();
?????? }finally {
?????????? if(session!=null){
????????????? session.close();
?????????? }
?????? }
??? }
??? public void testUpdate(){
?????? String hql="from TUser user where user.name=? ";
?????? Query query=session.createQuery(hql);
?????? query.setString(0, "uuu_sky");
?????? List list=query.list();
?????? TUser user=(TUser)list.get(0);
?????? System.out.println(user.getName());
?????? user.setName("liuzhiqiang");
?????? Transaction tran=session.beginTransaction();
?????? try{
?????????? session.update(user);
?????? ??? tran.commit();
?????? }catch(HibernateException e){
?????????? e.printStackTrace();
?????????? tran.rollback();
?????? }finally{
?????????? if(session!=null){
????????????? try{
????????????????? session.close();
????????????? }catch(HibernateException e){
????????????????? e.printStackTrace();
?????????????????
????????????? }
?????????? }
?????? }
???
??? }
???
?
}
最終的執行效果是向數據庫中插入數據 和修改數據庫中的現有數據都會將每次操作記錄進數據庫日志表中,保存當前操作的日志記錄。
?
posted on 2013-03-20 22:19 moonfans 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/moonfans/archive/2013/03/20/2972188.html
總結
以上是生活随笔為你收集整理的interceptor拦截器典型应用实例----数据稽核的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Linux 6上使用UDEV解决RAC
- 下一篇: C++ 创建一个窗口