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);
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。