{ protected Connection connection = null; protected String sqlStatement = ""; public JDBCAppenderExtended() { super(); } public void append(LoggingEvent event) { buffer.clear();// 清空缓冲器 buffer.add(event); flushBuffer();// 调用execute } protected void execute(String sql) throws SQLException { ThreadLocal myThread = (ThreadLocal) MDC.get("myThread"); for (Iterator i = buffer.iterator(); i.hasNext();) { LoggingEvent event = (LoggingEvent) i.next(); String message = (String) event.getMessage(); Map map = (Map) myThread.get(); Long packageid = Long.valueOf(map.get("packageid").toString()); Long userid = Long.valueOf(map.get("userid").toString()); Long operateType = (Long) map.get("operateType"); Connection con = null; PreparedStatement stmt = null; PreparedStatement stmt1 = null; ResultSet res = null; Statement myStmt=null; String sql2 = "select seq_logrecord_id.nextval from dual"; Long id = null; try { if (message.length() > 2000) { String msg = message.substring(0, 2000); con = getConnection(); myStmt = con.createStatement(); res = myStmt.executeQuery(sql2); if (res.next()) { id = res.getLong(1); } stmt = con.prepareStatement(sql); stmt.setLong(1, id); stmt.setLong(2, packageid); stmt.setLong(3, userid); stmt.setLong(4, operateType); stmt.setString(5, msg); stmt.executeUpdate(); String sql1 = "update logrecord set info=? where id=?"; stmt1 = con.prepareStatement(sql1); if (message.length() > 4000) { message = message.substring(0, 4000); } stmt1.setString(1, message); stmt1.setLong(2, id); stmt1.executeUpdate(); } else { con = getConnection(); myStmt = con.createStatement(); res = myStmt.executeQuery(sql2); if (res.next()) { id = res.getLong(1); } stmt = con.prepareStatement(sql); stmt.setLong(1, id); stmt.setLong(2, packageid); stmt.setLong(3, userid); stmt.setLong(4, operateType); stmt.setString(5, message); stmt.executeUpdate(); } } catch (SQLException e) { if (stmt != null) stmt.close(); if (stmt1 != null) stmt1.close(); if (res != null) res.close(); if(myStmt!=null) myStmt.close(); if(buffer!=null) buffer.clear(); closeConnection(con); throw e; } if (stmt != null) stmt.close(); if (stmt1 != null) stmt1.close(); if (res != null) res.close(); if(myStmt!=null) myStmt.close(); closeConnection(con); } } /** * Override this to return the connection , or to clean up the resource. * * The default behavior holds a single connection open until the appender is * closed (typically when garbage collected). */ 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); } } /** * */ public void setSql(String s) { sqlStatement = s; if (getLayout() == null) { this.setLayout(new PatternLayout(s)); } else { ((PatternLayout) getLayout()).setConversionPattern(s); } } /** * Returns pre-formated statement eg: insert into LogTable (msg) values * ("%m") */ public String getSql() { return sqlStatement; } } |
|