打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
log4j+数据库连接池添加自定义信息
public JDBCExtAppender() {
        super();
    }
   
    /**
     * 重载JDBCAppender的getConnection()方法
     */
    protected Connection getConnection() throws SQLException {

        if (DatabaseConfigure.getInstance().getDatapool() != null) {
            try{
            Context initCtx = new InitialContext();

            DataSource ds = (DataSource) initCtx.lookup(DatabaseConfigure.getInstance().getDatapool());
            if (ds != null)
                this.connection = ds.getConnection();
            }catch(NamingException namingex){
                namingex.printStackTrace();
                throw new SQLException("-datapool init error ");
            }
        } else {
           
            //如果没有数据库连接池,则直接连接
            try {
                Class.forName(DatabaseConfigure.getInstance().getJdbcDriver());
            } catch (ClassNotFoundException e) {
                System.out.println(" class not found: " + e.getMessage());
                e.printStackTrace();
                throw new SQLException("-Database driver notFind ");
            }

            try {
                this.connection = DriverManager.getConnection(DatabaseConfigure.getInstance()
                        .getDatabaseURL(), DatabaseConfigure.getInstance()
                        .getDatabaseName(), DatabaseConfigure.getInstance()
                        .getDatebasePassword());
            } catch (SQLException sqlex) {
                System.err.println("DatabaseBean connection error"
                        + sqlex.getMessage());
                sqlex.printStackTrace();
                throw new SQLException("-Database connection error ");
            }
        }
        return this.connection;
    }
   
   
   
    /**
     * 重载getLogStatement()方法,
     * 在SQL字符串最后添加用户ID等信息
     */
     protected String getLogStatement(LoggingEvent event) {
         StringBuffer stringBuffer = new StringBuffer();
         stringBuffer.append(layout.format(event));
         
         if (event.getMessage() instanceof ParameterizedMessage) {
             
             //检测SQL的最后一个字符是不是逗号,如果不是,则在这里补上
             if(stringBuffer.charAt(stringBuffer.length()-1)!=',')
                 stringBuffer.append(",");
             
             Object[] params = ((ParameterizedMessage) event.getMessage()).getParameters();  
             for (int i = 1; i < params.length; i++) {
                     stringBuffer.append(params[i]);
                     stringBuffer.append(","); 
             }  
            
             stringBuffer.deleteCharAt(stringBuffer.length()-1);
             stringBuffer.append(")");
         }  
        return stringBuffer.toString();
    }
     
   
    /**
     * 重载JDBCAppender的方法,取消与数据库的连接
     */
    protected void closeConnection(Connection con) {
       
        try {
            if (connection != null && !connection.isClosed())
                connection.close();
        } catch (SQLException e) {
            errorHandler.error("Error closing connection", e,
                    ErrorCode.GENERIC_FAILURE);
        }

    }
   
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
数据库连接池 dbcp与c3p0的使用
韩顺平的SqlHelper类
使用JDBC连接数据库 - 《Java JDK 6学习笔记》
6、MD5加密
使用JAVA中的动态代理实现数据库连接池
《深入了解mybatis原理》 Mybatis数据源与连接池
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服