中间件服务断数据库会话关闭代码(java)

*****************************************Java类******************************************

package com.taiji.waf.filter;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import  javax.servlet.ServletContextAttributeEvent; 
import  javax.servlet.ServletContextAttributeListener; 
import  javax.servlet.ServletContextEvent; 
import  javax.servlet.ServletContextListener; 

import com.taiji.waf.WAFConfigure;


/** 
 * ServletContext对象监听器.<br> 
 * 作用:监听ServletContext对象生命周期及ServletContext对象中属性变化情况 
 *  
 */ 
public   class  ContextListener  implements  ServletContextListener, 
        ServletContextAttributeListener { 

     /** 
     * 当应用关闭时将执行此方法 
     */ 
     public   void  contextDestroyed(ServletContextEvent arg0) {
             System.out.println( "【监听到】应用被关闭!" );
             closeDataConn();
    } 

     /** 
     * 当应用启动时将执行此方法 
     */ 
     public   void  contextInitialized(ServletContextEvent arg0) { 
         System.out.println( "【监听到】应用被启动!" ); 
    } 

     /** 
     * 当ServletContext对象中新增属性时将执行此方法 
     */ 
     public   void  attributeAdded(ServletContextAttributeEvent arg0) { 
         System.out.println( "【监听到】ServletContext对象中新增一名为"  + arg0.getName() 
                +  "的属性,其属性值为:"  + arg0.getValue()); 
    } 

     /** 
     * 当ServletContext对象中删除属性时将执行此方法 
     */ 
     public   void  attributeRemoved(ServletContextAttributeEvent arg0) { 
         System.out.println( "【监听到】ServletContext对象中一名为"  + arg0.getName() 
                +  "的属性被删除!" ); 
    } 

     /** 
     * 当ServletContext对象中更新属性时将执行此方法 
     */ 
     public   void  attributeReplaced(ServletContextAttributeEvent arg0) { 
         System.out.println( "【监听到】ServletContext对象中一名为"  + arg0.getName() 
                +  "的属性被更新!" ); 
    } 
     /**
      * 获得计算机名称
      * @return
      */
     public static String getHostName(){  
         String hostName="";
         try{  
             InetAddress addr = InetAddress.getLocalHost();   
             hostName=addr.getHostName().toString(); //获取本机计算机名称   

         }catch(Exception e){  
             e.printStackTrace();  
         }  
         return hostName;
     }
     /**
      * 关闭所有数据库连接
      */
     private static void closeDataConn() {
        String name=getHostName();
        String sql=closeDataConn(name);
        String driverClass = WAFConfigure.getProperty("Broker_driver");
        String url = WAFConfigure.getProperty("Broker_url");
        String user= WAFConfigure.getProperty("Broker_username");
        String password = WAFConfigure.getProperty("Broker_password");
        Connection conn =null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url,user,password);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.executeUpdate();
            pstmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }



    /**
     * 拼写关闭所有当前用户建立的数据库联接SQL
     * @param ownuserId
     */
    public static String  closeDataConn(String ownuserId){
        String sql=" declare cursor cur_sess is select sid, serial# from v$session where status = 'INACTIVE'";
        sql+=" and type  != 'BACKGROUD' and (machine=? or machine='localhost');  w_sid number; w_serial number; begin open cur_sess; loop fetch cur_sess into w_sid,w_serial; if cur_sess%notfound then exit;  end if;";
        sql+=" execute immediate 'alter system kill session '''||w_sid||','||w_serial||''''; end loop; end;";
        return sql;
    }
} 

*****************************************相应的配置要求******************************************

Web.xml应添加

 <listener>
         <listener-class>
            com.taiji.waf.filter.ContextListener
         </listener-class>
 </listener>

类中的数据库连接是通过读取property文件内容得到的:

Broker_driver=oracle.jdbc.driver.OracleDriver
Broker_url=jdbc:oracle:thin:@172.25.13.102:1521:dxoa1
Broker_username=dxmh
Broker_password=dxmh


改成和自己项目相应的数据库连接和用户的取法即可。

 

posted @ 2013-01-15 18:08  冰镇橘子汽水  阅读(631)  评论(0编辑  收藏  举报