中间件服务断数据库会话关闭代码(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


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

 

转载于:https://www.cnblogs.com/is1988/archive/2013/01/15/2861658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值