heting
BlogJava
::
首頁
::
新隨筆
::
聯系
::
聚合
::
管理
::
40 隨筆 :: 9 文章 :: 45 評論 :: 0 Trackbacks
<
2009年1月
>
日
一
二
三
四
五
六
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
c#范例(4)
(rss)
java范例
(rss)
js(3)
(rss)
linux(1)
(rss)
WebSphere(1)
(rss)
數據庫(3)
(rss)
隨筆檔案
2010年10月 (1)
2010年8月 (5)
2010年3月 (2)
2009年11月 (2)
2009年9月 (1)
2009年8月 (1)
2009年7月 (2)
2009年5月 (1)
2009年4月 (3)
2009年3月 (4)
2009年2月 (1)
2009年1月 (3)
2008年12月 (2)
2008年11月 (7)
2008年10月 (4)
2008年9月 (1)
文章檔案
2008年9月 (9)
搜索
最新評論
1.?re: struts2+freemarker中防止表單的重復提交token
@51互聯云-濟南程序猿
你什么都不懂,瞎說
--人
2.?re: struts2+freemarker中防止表單的重復提交token
沒關系,大家好,什么都沒有
--人
3.?re: struts2+freemarker中防止表單的重復提交token
套頭
--人
4.?re: struts2+freemarker中防止表單的重復提交token
評論內容較長,點擊標題查看
--51互聯云-濟南程序猿
5.?re: struts1文件上傳和下載
評論內容較長,點擊標題查看
--zuidaima
閱讀排行榜
1.?struts1文件上傳和下載(21131)
2.?javax.naming.CommunicationException 的一個相關異常(已解決)(11551)
3.?自己寫的一個c#winform打印類(8505)
4.?C# 與 C++ 數據類型比較及結構體轉換 (7217)
5.?WebSphere7.0 上部署struts2 找不到用于處理 JSP 的擴展處理器(2983)
評論排行榜
1.?struts1文件上傳和下載(23)
2.?自己寫的一個c#winform打印類(8)
3.?struts2+freemarker中防止表單的重復提交token(6)
4.?Ireport在瀏覽器中的顯示代碼(2)
5.?EJ3.0將EJB程序和WEb程序發布到weblogic10.3是出現的錯誤weblogic.wsee.async.AsyncResponseBean(2)
資源回收
void
f();
{
Cleaner cleaner
=
new
Cleaner();;
try
{
Connection conn
=
;
cleaner.add(conn);;
Statement stmt
=
conn.createStatement();;
cleaner.add(stmt );;
ResultSet rset
=
;
cleaner.add(rset );;
//
using ResultSet
cleaner.clean();
//
close rset
}
catch
(Exception e);
{
throw
new
RuntimeException(
""
,e);;
}
finally
{
cleaner.cleanAll();;
}
}
import
java.io.InputStream;
import
java.lang.reflect.Method;
import
java.net.Socket;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.Statement;
import
java.util.HashMap;
import
java.util.Iterator;
import
java.util.Map;
import
java.util.Stack;
/** */
/**
*
@author
pufan
*
*/
public
final
class
Cleaner
{
public
static
interface
CleanListener
{
void
catchClean(Object obj, Throwable t);;
}
private
final
static
CleanListener NONE_LISTENER
=
new
CleanListener();
{
public
void
catchClean(Object obj, Throwable t);
{
}
}
;
private
final
static
Map map
=
new
HashMap();
{
{
put(Connection.
class
,
"
close
"
);;
put(Statement.
class
,
"
close
"
);;
put(PreparedStatement.
class
,
"
close
"
);;
put(ResultSet.
class
,
"
close
"
);;
put(InputStream.
class
,
"
close
"
);;
put(Socket.
class
,
"
close
"
);;
}
}
;
private
final
Stack garbageContainer
=
new
Stack();;
public
void
add(Object garbage);
{
add(garbage, NONE_LISTENER);;
}
public
void
add(Object garbage, CleanListener listener);
{
garbageContainer.add(
new
Garbage(garbage, listener););;
}
public
void
clean();
{
if
(garbageContainer.size();
>
0
);
try
{
doClean();;
}
catch
(Throwable e);
{
throwThrowable(e);;
}
}
private
void
doClean();
throws
Throwable
{
Garbage garbage
=
(Garbage); garbageContainer.pop();;
garbage.clean();;
}
public
void
cleanAll();
{
Throwable throwable
=
null
;
while
(garbageContainer.size();
>
0
);
try
{
doClean();;
}
catch
(Throwable t);
{
if
(throwable
==
null
);
throwable
=
t;
}
throwThrowable(throwable);;
}
private
void
throwThrowable(Throwable throwable);
{
if
(throwable
==
null
);
return
;
if
(throwable
instanceof
Error);
throw
(Error); throwable;
else
throw
new
RuntimeException(
"
error in closing garbage!
"
, throwable);;
}
private
static
Method getMethod(Object obj);
{
Class clazz
=
obj.getClass();;
String methodName
=
getMethodName(clazz);;
try
{
return
clazz.getMethod(methodName,
new
Class[
0
]);;
}
catch
(Exception e);
{
throw
new
RuntimeException(
"
can't find close method by
"
+
obj, e);;
}
}
private
static
String getMethodName(Class clazz);
{
for
(Iterator it
=
map.keySet();.iterator();; it.hasNext();; );
{
Class c
=
(Class); it.next();;
if
(c.isAssignableFrom(clazz););
return
(String); map.get(c);;
}
return
null
;
}
private
static
class
Garbage
{
Object obj;
Method method;
CleanListener listener;
Garbage(Object obj, CleanListener listener);
{
method
=
getMethod(obj);;
this
.obj
=
obj;
this
.listener
=
listener;
}
void
clean();
throws
Throwable
{
try
{
method.invoke(obj,
new
Object[
0
]);;
}
catch
(Throwable t);
{
listener.catchClean(obj, t);;
throw
t;
}
}
}
}
posted on 2009-01-10 10:01
賀挺
閱讀(178)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 賀挺
主站蜘蛛池模板:
东阳市
|
双峰县
|
阳信县
|
达州市
|
信丰县
|
清苑县
|
浦东新区
|
日土县
|
梓潼县
|
崇州市
|
崇阳县
|
扎鲁特旗
|
斗六市
|
偃师市
|
万荣县
|
宁陵县
|
白城市
|
元阳县
|
闸北区
|
吉隆县
|
宜兰市
|
东平县
|
汾阳市
|
吕梁市
|
凤阳县
|
建阳市
|
邵阳县
|
大英县
|
田阳县
|
镇沅
|
三门县
|
长宁县
|
西昌市
|
山丹县
|
郁南县
|
林周县
|
眉山市
|
泰宁县
|
衡阳县
|
四平市
|
北流市
|