這幾天做項目,需要做一個類似于控制臺的界面,可以將日志回顯到一個文本區(qū)域里,幾經查找,反復測試寫了以下測試代碼。
主程序
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Writer;

import org.apache.log4j.Appender;
import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;


public class Log4jMain
{
static public Logger logger = Logger.getLogger(Log4jMain.class);


public static void main(String[] arg)
{
Logger root = Logger.getRootLogger();

try
{
Appender appender = root.getAppender("WriterAppender");
PipedReader reader = new PipedReader();
Writer writer = new PipedWriter( reader) ;
((WriterAppender) appender).setWriter(writer);
Thread t = new AaaThread(reader);
t.start();
Logger logger = Logger.getLogger(Log4jMain.class);
logger.error("asdf");
logger.info("asdf");
logger.debug("asdf");
logger.fatal("asdf");
//hh是一個class,test方法里只有幾個logger.info("aaa"),用于測試用,這東西可以去掉
new HH().test();


} catch (Exception e)
{
}

}
}
因為log4j提供的是 Writer,所以通過使用PipedWriter連接到PipedReader,并通過一個thread來處理PipedReader的數(shù)據(jù)就可以回顯了。如果你需要的話可以將下面的System.out.println(scanner.nextLine());改成你需要顯示的地方就行了。
import java.io.PipedReader;
import java.util.Scanner;


public class AaaThread extends Thread
{
PipedReader reader;


public AaaThread(PipedReader reader)
{
this.reader = reader;
}


public void run()
{
Scanner scanner = new Scanner(reader);

while (scanner.hasNext())
{
System.out.println(scanner.nextLine());
}
}
}
posted @
2008-06-06 09:31 小平 閱讀(1224) |
評論 (0) |
編輯 收藏
這里制作一個引子,具體內容比較多,而且hibernate文檔里的hql篇寫的很詳細
可以這么用:
List list = getHibernateTemplate().find("select new map(t1.c1,t2.c3) from table1 t1 ,table2 t2 where t1.c1=t2.c2");
這樣的話list里的每個元素都是一個map,每個map里包含兩個元素
注意:這里的table1和table2都是class名并不是真的表名,畢竟這是hql。除了可以用map還還支持list和自定義的bean。
posted @
2008-04-17 17:05 小平 閱讀(1893) |
評論 (0) |
編輯 收藏
DB2的Type-4驅動真的很詭異,配了兩天沒配上,發(fā)現(xiàn)是多引了一個包。就是他----“db2java.zip(db2java.jar)”只要有了他就算你已經引入了db2jcc.jar、db2jcc_license_cisuz.jar、db2jcc_license_cu.jar也沒用。
不知道怎么回事,本以為多引就沒錯,沒想到載這了。
順便吧db2 type-4的配置方法貼下面:
jdbc:db2://ip:port/數(shù)據(jù)庫
驅動名:com.ibm.db2.jcc.DB2Driver
我日他IBM!該死的IBM,我算是服了,配好的type-4驅動中文數(shù)據(jù)顯示的全是亂碼,準備切到type-2上試試,結構還是不行,查來查去是數(shù)據(jù)庫pagecode的事,數(shù)據(jù)庫的字符集改成gbk,客戶端的pagecode也改成跟數(shù)據(jù)庫一樣的,ODBC算是能用了,type-2也能用了,然后又用type-4一試!連不上了!說字符集不匹配,上網(wǎng)一查說要使用IBM專用的JDK,我日了,換了IBM的JDK,配好環(huán)境變量eclipse又不能用了!我算是被IBM搞死了,一氣之下也不準備再嘗試type-4了。
我這里用的IBM JDK1.4,db2 V8
posted @
2008-04-17 16:51 小平 閱讀(2525) |
評論 (3) |
編輯 收藏
近日抽風,想了解以下java與串口并口通訊的問題,上網(wǎng)查了半天,找到了sun幾年前的一個破包,comm.jar
2.0版的,遇到一個比較奇怪的問題,這個包里的demo程序用cmd可以運行,但是用eclipse編譯就不能運行,一直以為是dll庫和配置文件的問題,幾乎將兩個文件copy到了系統(tǒng)中全部可能出現(xiàn)的地方都沒有解決。百般無奈之下,找到了
http://blog.csdn.net/djsl6071/archive/2007/04/25/1583979.aspx這個博客,下載了rxtx把問題搞定了。
posted @
2008-04-17 16:40 小平 閱讀(574) |
評論 (0) |
編輯 收藏
如果RMI客戶端鏈接補上服務器可能是以下情況導致的:
1、網(wǎng)絡不通
2、端口占用
3、防火墻(客戶端機器的防火墻和服務器端的防火墻都有可能)
4、服務器端所在機器安裝了linux虛擬機,或者使用的linux或者unix系統(tǒng)
排除辦法:
前三種情況都很好解決,主要是最后一個,需要在rmi服務啟動時加上一句
java -Djava.rmi.server.hostname=localhost ……
其中l(wèi)ocalhost可以強制定義為機器的IP地址
posted @
2008-02-15 14:11 小平 閱讀(886) |
評論 (0) |
編輯 收藏
為了滿足將plugin工程打成jar包,但是spring的配置文件不打進jar包的需求。很多時候我們?yōu)榱丝梢苑奖闶止づ渲胹pring的一些信息,而不用將jar解壓而從新壓縮,我們需要把Spring的配置文件放在plugin的jar包外這時,如果沒有正確的使用spring的Resource就不能加載spring的配置文件,經過多次嘗試終于找到一個比較合適的方法,與大家交流。

public static BeanFactory getFactory()
{

if (factory == null)
{
FileSystemResource resource = new FileSystemResource("appcontextclient.xml");
factory = new XmlBeanFactory(resource);
}
return factory;

}
將appcontextclient.xml文件放到工程根目錄下,發(fā)布時將appcontextclient.xml放入到你的eclipse的安裝目錄即可(與eclipse的exe執(zhí)行文件相同的位置),如果需要可在appcontextclient.xml文件前加入相關的路徑來更改你的目錄(但是有可能在調試過程中必須要把appcontextclient.xml文件放到你的eclipse IDE的安裝目錄里,因為大部分時間我們的eclipse IDE是何 workspace分開存放的,這樣我們在調試的時候非常別扭,要到IDE里去修改配置文件)。
以上方法并不是唯一的,只是我感覺用起來比較合適的,這樣在調試時既可以保證appcontextclient.xml文件在工程目錄范圍內,有可以在發(fā)布時使文件保持在安裝目錄而不是運行環(huán)境的workspace的臨時目錄里。
同時介紹幾個方法可以eclipse方法獲得eclipse工具的幾個重要路徑(注:"IDE"為我的plugin工程的PLUGIN_ID)
Platform.getInstallLocation().getURL()
file:/F:/tools/java/eclipse/eclipse-SDK-3.3-win32/eclipse/
這是我的eclipse IDE的安裝目錄
Platform.getLocation()
D:/runtime-IDE.product
這是我調試插件時生成的臨時運行環(huán)境目錄,是在調試IDE工程時生成的,就是發(fā)布時插件所屬eclipse運行環(huán)境的WorkSpace目錄
Platform.getInstanceLocation().getURL()
file:/D:/runtime-IDE.product/
這個跟上面的一樣
Platform.getLocation()
D:/runtime-IDE.product
這個跟上面的也一樣
Platform.getLogFileLocation()
D:/runtime-IDE.product/.metadata/.log
運行環(huán)境的日志文件路徑,發(fā)布時插件所屬eclipse運行環(huán)境的WorkSpace目錄下的/.metadata/.log
Platform.getStateLocation(Platform.getBundle("IDE"))
D:/runtime-IDE.product/.metadata/.plugins/IDE
運行環(huán)境中IDE的一個臨時目錄,發(fā)布時插件所屬eclipse運行環(huán)境的WorkSpace目錄下/.metadata/.plugins/IDE
Platform.getUserLocation()
file:/C:/Documents and Settings/XX/user
這是我的文檔的路徑(把用戶名X掉,哈哈)
Platform.getBundle("IDE").getLocation()
update@D:/WorkSpace/IDE/
我的IDE工程的路徑,對應發(fā)布時插件的jar本身,在這個目錄下的文件將全部包含在發(fā)不得jar里。
Platform.getConfigurationLocation().getURL()
file:/D:/WorkSpace/.metadata/.plugins/org.eclipse.pde.core/IDE.product/
eclipse IDE 自己建的目錄
eclipse 本身也有很多加載資源文件的方法可以在org.eclipse.core.runtime.Platform這個類里找到,具體的就請大家找找API吧
posted @
2008-01-05 21:35 小平 閱讀(2013) |
評論 (0) |
編輯 收藏
應要求在此提供ibatis批量插入demo下載,謝謝大家的關注,不過對于一些人的不禮貌行為我非常痛心。由于工作地點不能上網(wǎng),不能上網(wǎng),不能及時回復大家的留言我深表道歉。
下載
posted @
2007-12-27 19:52 小平 閱讀(3420) |
評論 (9) |
編輯 收藏
此問題網(wǎng)上大部分解決方法是這樣的:
1.網(wǎng)上鄰居->本地連接->屬性->internet協(xié)議(TCP/IP)->屬性->高級->wins標簽->去掉啟用LMhosts查詢前的勾.
2.控制面版->windows防火墻->高級標簽->本地連接設置->服務的標簽里勾選安全Web服務器(HTTPS)即可
但是我的機器按照上面操作apache服務仍然不可以使用,于是索性將服務中的Windows Firewall/Internet Connection Sharing (ICS的服務關掉了,發(fā)現(xiàn)問題解決了,apache可以用了。對于安全問題可以選擇第3方的防火墻來解決。
注意:費爾防火墻好像和apache沖突,也會導致WSASocket failed to open the inherited socket
posted @
2007-10-21 14:53 小平 閱讀(1462) |
評論 (1) |
編輯 收藏
今天用sun的jdk調的沒有一點問題的數(shù)據(jù)抽取程序,方放到AIX上后不能用了,oralce報出向一個字段中插入了過長的數(shù)據(jù),可是程序在幾天前還是沒有問題的,后來分析可能是字符集導致的,因為前幾天修改過informix數(shù)據(jù)庫的字符集由8859-1轉為了zh_cn.gb18030-2000,但解決過程異常艱難,后來吧informix的url改為<value>jdbc:informix-sqli://xx.xx.xx.xx:8888/XXXX:informixserver=whcspdev;DB_LOCALE=zh_cn.gb18030-2000;CLIENT_LOCALE=zh_cn.UTF8(原為zh_cn.gb18030-2000);NEWCODESET=gb18030,gb18030-2000,5488,utf8</value>就可以了。后來猜測了一下原因:
1、oracle數(shù)據(jù)庫的jdbc驅動是是自動將數(shù)據(jù)庫的數(shù)據(jù)轉為unicode格式,這可能跟java的String默認為unicode有關,為了讓大家方便操作,
2、sun jdk的String在初始時用的是Unicode格式,ibm的jdk應該也是,但是有一點,sun的jdk在通過informix jdbc驅動取數(shù)據(jù)時不會將數(shù)據(jù)轉成其他字符集,而是仍然使用Unicode,而IBM的則是通過CLIENT_LOCALE=zh_cn.gb18030-2000配置將數(shù)據(jù)格式轉為了zh_cn.gb18030-2000。
所以在oracle作insert的時候就會由于字符集不匹配而報錯
posted @
2007-07-18 19:23 小平 閱讀(998) |
評論 (2) |
編輯 收藏
前兩天遇到為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能這個問題,搜遍了國內網(wǎng)站,也沒有個好結果,終于多天后在一個國外的svn服務器上找到了一段代碼,解決了問題
1、為TextViewer添加TextViewerUndoManager來管理記錄
protected TextViewer textViewer;
protected TextViewerUndoManager undoManager;
//20是保存記錄的數(shù)量。
undoManager = new TextViewerUndoManager(20);
//綁定對textViewer控件的數(shù)據(jù)進行管理
undoManager.connect(textViewer);
textViewer.setUndoManager(undoManager);


2、添加Ctrl+z,與Ctrl+y事件
StyledText styledText = textViewer.getTextWidget();

styledText.addKeyListener(new KeyListener( )
{
public void keyPressed( KeyEvent e )

{
if ( isUndoKeyPress( e ) )

{
textViewer.doOperation( ITextOperationTarget.UNDO );
}
else if ( isRedoKeyPress( e ) )

{
textViewer.doOperation( ITextOperationTarget.REDO );
}
}
private boolean isUndoKeyPress( KeyEvent e )

{
// CTRL + z
return ( ( e.stateMask & SWT.CONTROL ) > 0 )
&& ( ( e.keyCode == 'z' ) || ( e.keyCode == 'Z' ) );
}
private boolean isRedoKeyPress( KeyEvent e )

{
// CTRL + y
return ( ( e.stateMask & SWT.CONTROL ) > 0 )
&& ( ( e.keyCode == 'y' ) || ( e.keyCode == 'Y' ) );
}
public void keyReleased( KeyEvent e )

{
// do nothing
}
});
posted @
2007-07-16 10:49 小平 閱讀(1870) |
評論 (3) |
編輯 收藏
到一個這東西都寫到3了,針對上回說到30000條數(shù)據(jù)的批量插入工作。30000條數(shù)據(jù)的批量插入在一個事務里處理固然是快,但是這只是測試環(huán)境,30000條數(shù)據(jù)在數(shù)據(jù)庫的緩存里必然對數(shù)數(shù)據(jù)庫的緩存和鎖數(shù)量都是一個大的挑戰(zhàn),固在新的程序中我們使用了分批事務提交的方式,這樣為了保持數(shù)據(jù)的正確行就只能人為控制數(shù)據(jù)庫中已被插入的數(shù)據(jù)是否delete掉。另外,使用Batch塊提交會引發(fā)一個問題就是,如果batch塊中發(fā)生了異常,我們得不到異常數(shù)據(jù)的行號即任何信息,所以只能是魚和熊掌不可兼得(我已關注過insert方法中返回pk的方法了,但好像在batch中他反回不了出錯的行號,也許是我沒有找到方法,如有人有好方法請共享一下,在這里表示感謝),大家酌情考慮吧,只能到到自己需要的平衡點了。
建議:如果對數(shù)據(jù)的準確性毋庸置疑的話就是用batch處理。如果不能確定準確性的話,如果對那條數(shù)據(jù)出錯無所謂的話就也可以用batch,但是非要返回出錯行號的話就不要用batch了,直接在外面套用一個事務,然后try catch一下,處理一下行號。
posted @
2007-06-28 21:32 小平 閱讀(3290) |
評論 (4) |
編輯 收藏
今天發(fā)生了ORA-01461 :can bind a LONG value only for insert into a LONG ...到處查詢發(fā)現(xiàn)是一個字段中的中文內容導致,仔細分析了一下原因,是因為jdk1.5的String類型為utf-16編碼方式,而jdk1.4為utf-8,通過在oracle網(wǎng)上查詢10g的jdbc驅動有兩個版本,舊版的不持jdk1.5,只支持jdk1.4,通過換jdbc驅動問題解決。
posted @
2007-06-20 10:53 小平 閱讀(2803) |
評論 (0) |
編輯 收藏
1、上回的心得中我強調了startBatch()的批處理的作用,但是其中的使用是個錯誤用法,并沒有發(fā)揮出startBatch()的實力,對此給與觀眾的誤導我將在此表示到欠,并貼出正確的用法

public class LocalDaoImpl extends SqlMapClientDaoSupport implements LocalDao
{

public void insertBuNaTaxBatLst(final PaginatedList list)

{

getSqlMapClientTemplate().execute(new SqlMapClientCallback()
{
public Object doInSqlMapClient(SqlMapExecutor executor)

throws SQLException
{
executor.startBatch();
// do some iBatis operations here
for(int i=0,count=list.size();i<count;i++)

{
executor.insert("insertBuNaTaxBatLst", list.get(i));

if (i % 50 == 0)
{
System.out.println("----" + i);//沒有意義只為測試
}
}
executor.executeBatch();
return null;
}
});
}

}
這樣才能利用上startBatch()威力。
2、注意ibatis的事物默認情況下是自動提交的,如果發(fā)現(xiàn)速度上有問題可以留意一下,ibatis只有在顯示的聲明事物管理的情況下才自動將事物管理改為不自動方式。
3、還是startBatch(),據(jù)我測試分析這個鬼東西只有在executeBatch(),才把所有的語句提交到數(shù)據(jù)庫,在提交之前緩存中保留了大量的sql語句和數(shù)據(jù)對象,很有可能out of memony,對此要留意,可以在大量數(shù)據(jù)要做插入時,分批用Batch,如:有40000條數(shù)據(jù)可將其分為4個Batch塊,讓后將這4個Batch用一個事物提交以保證數(shù)據(jù)完整性。
注:最近在做數(shù)據(jù)抽取項目,愿與大家溝通心得
posted @
2007-05-30 21:46 小平 閱讀(5962) |
評論 (6) |
編輯 收藏
程序功能:
使用ibatis+spring將oracle數(shù)據(jù)庫中的tfile表中的數(shù)據(jù)抽取到db2數(shù)據(jù)庫的tfile表,這兩個表的結構相同。
測試環(huán)境:
celeron M 1.4/512M/mysql 5.0數(shù)據(jù)庫
代碼:

public static void main(String[] args)
{
// TODO Auto-generated method stub
ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
TFileDAO tFileDao = (TFileDAO) factory.getBean("tfile");

TFileDAO test2FileDao = (TFileDAO) factory.getBean("test2tfile");
//獲取全部數(shù)據(jù)
List list = tFileDao.getAll();
//開啟事務
DataSourceTransactionManager txm = (DataSourceTransactionManager) factory
.getBean("test2transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = txm.getTransaction(def);

try
{
test2FileDao.getSqlMapClient().startBatch();

for (int i = 0, count = list.size(); i < count; i++)
{
//插入數(shù)據(jù)
test2FileDao.insert((TFile) list.get(i));
}
test2FileDao.getSqlMapClient().executeBatch();// 這兩句有問題,請見Spring+ibatis心得2!

} catch (Exception e)
{
txm.rollback(status);
System.out.println(e);
}
txm.commit(status);
System.out.println(list.size());
}
1、保證使用長事務,不要在每個插入都事務提交,這樣性能可以有很大幅度的提升
2、使用 test2FileDao.getSqlMapClient().startBatch();
test2FileDao.getSqlMapClient().executeBatch();
可以發(fā)起jdbc對批量數(shù)據(jù)插入的優(yōu)化與自動代碼壓縮功能。
結語:這次使用ibatis在同樣的硬件、數(shù)據(jù)庫、數(shù)據(jù)條數(shù)的環(huán)境下測試,在不起用batch,所有數(shù)據(jù)庫,數(shù)據(jù)池特性均使用默認設置情況下使用19秒,并且使用一次性將數(shù)據(jù)讀入內存的方式,效果優(yōu)于hibernate,所以真信優(yōu)化后的程序應該比hibernate效率更高。但是從程序編寫方面來講,hibernate省去了過多的代碼,可以讓程序員更輕松些。
posted @
2007-04-26 19:54 小平 閱讀(3203) |
評論 (1) |
編輯 收藏
程序功能:
使用hibernate+spring將oracle數(shù)據(jù)庫中的tfile表中的數(shù)據(jù)抽取到db2數(shù)據(jù)庫的tfile表,這兩個表的結構相同。(原本要使用一些Spring的特性,但是程序改來改去發(fā)現(xiàn)Spring特性一個都沒用上,實際上完全可以由hibernate創(chuàng)建兩個sessionFactory完成)
測試環(huán)境:
celeron M 1.4/512M/mysql 5.0數(shù)據(jù)庫
代碼:

public void save()
{
Session session= fileDAO.getDataSession();
Session session2= fileDAO2.getDataSession();
Transaction tx =session2.beginTransaction();
int count=0;
List list =fileDAO.getList(session, count);
while(list.size()!=0)

{
for(int i=0,num =list.size();i<num;i++)

{
session2.save(list.get(i));
session.evict(list.get(i));
if(num%50==0)

{
session2.flush();
session2.clear();
}
}
count= count+500;
list =fileDAO.getList(session, count);
// System.out.println(count);
}
tx.commit();
session.close();
session2.close();
}

配置文件:
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
1、為保證不會出現(xiàn)內存溢出
hibernate.jdbc.batch_size 設為 20-50
并在代碼中沒隔50個insert后手工清理數(shù)據(jù)
if(num%50==0)

{
session2.flush();
session2.clear();
}

2、為保證減少二級緩存導致的過多的內存開銷關,閉二級緩存
hibernate.cache.use_second_level_cache 設為false
3、保證使用長事務,不要在每個插入都事務提交,這樣性能可以有很大幅度的提升(由于原先配的事務沒有正常運行,在初次測試時此程序插入4萬條數(shù)據(jù)用了12分鐘,使用了長事務后僅為34秒)
4、使用ScrollableResults(提供了數(shù)據(jù)庫的游標特性)然后插入的效果好像要優(yōu)于分批抓取分批插入的效果,(4萬條數(shù)據(jù),用ScrollableResult耗時29秒)但網(wǎng)上有人聲稱批量抓取插入的效果要好可能在遠程數(shù)據(jù)庫的情況下批量抓取的可靠性更高一點的原因。這一點我詢問過公司里做數(shù)據(jù)庫比較好的人,批量處理數(shù)據(jù)是要使用游標處理的。就游標而言分為動態(tài)游標,靜態(tài)游標,動態(tài)游標慢于靜態(tài)游標,靜態(tài)游標慢于靜態(tài)查詢,但是如果用分批抓取數(shù)據(jù)的話就涉及到數(shù)據(jù)分段截取,為保證每次分段截取時數(shù)據(jù)的正確性,應該要預先對數(shù)據(jù)處理,所以批量抽取數(shù)據(jù)的速度可能會慢一些。以下為使用ScrollableResult的程序
Session session= fileDAO.getDataSession();
Session session2= fileDAO2.getDataSession();
Transaction tx =session2.beginTransaction();
ScrollableResults tFiles = session.createQuery(
"from TFile as model ").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
int count=0;
while(tFiles.next())

{
session2.save(tFiles.get(0));
if(++count%50==0)

{
session2.flush();
session2.clear();
}
}
tx.commit();
session.close();
session2.close();
posted @
2007-04-24 19:30 小平 閱讀(1799) |
評論 (0) |
編輯 收藏
1、定義靜態(tài)的StatusLine文本
WorkbenchWindowAdvisor 類中的preWindowOpen()方法加入
configurer.setShowStatusLine(true);//顯示狀態(tài)欄
ApplicationActionBarAdvisor類中增加
protected void fillStatusLine(IStatusLineManager statusLine) {
super.fillStatusLine(statusLine);
StatusLineContributionItem statusItem = new StatusLineContributionItem("DAStatus",50);
statusItem.setText("狀態(tài)欄:測試");
statusLine.add(statusItem);
}
將在狀態(tài)欄中顯示:“狀態(tài)欄:測試”
2、定義動態(tài)的StatusLine文本
WorkbenchWindowAdvisor 類中的preWindowOpen()方法加入
configurer.setShowStatusLine(true);
在要調用狀態(tài)欄的class里加入下面方法
private void showStatusMessage(String msg) {
WorkbenchWindow workbenchWindow = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IStatusLineManager lineManager = workbenchWindow.getStatusLineManager();
StatusLineContributionItem statusItem = new StatusLineContributionItem("DAStatus",50);
statusItem.setText(msg);
lineManager.add(statusItem);
}
使用listener來調用showStatusMessage(String msg)即可。
posted @
2007-04-09 15:53 小平 閱讀(1384) |
評論 (1) |
編輯 收藏
<!-- 前置數(shù)據(jù)源MCP ? -->
<bean id="dataSourceMcp" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.informix.jdbc.IfxDriver</value>
</property>
<property name="url">
<value>jdbc:informix-sqli://192.168.102.31:8888/csptestdb:informixserver=whcspdev;db_locale=zh_CN.gb</value>
</property>
<property name="username">
<value>csptest</value>
</property>
<property name="password">
<value>csptest</value>
</property>
</bean>
程序
ClassPathResource resource = new ClassPathResource("resource/appcontextserver.xml");
factory = new XmlBeanFactory(resource);
BasicDataSource o = (BasicDataSource) factory.getBean("dataSourceMcp")
o就是存放數(shù)據(jù)庫信息的實例
posted @
2007-04-09 15:41 小平 閱讀(876) |
評論 (0) |
編輯 收藏
<c3p0-config> <default-config> <!--當連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù)。Default: 3 --> <property name="acquireIncrement">3</property>
<!--定義在從數(shù)據(jù)庫獲取新連接失敗后重復嘗試的次數(shù)。Default: 30 --> <property name="acquireRetryAttempts">30</property>
<!--兩次連接中間隔時間,單位毫秒。Default: 1000 --> <property name="acquireRetryDelay">1000</property>
<!--連接關閉時默認將所有未提交的操作回滾。Default: false --> <property name="autoCommitOnClose">false</property>
<!--c3p0將建一張名為Test的空表,并使用其自帶的查詢語句進行測試。如果定義了這個參數(shù)那么 屬性preferredTestQuery將被忽略。你不能在這張Test表上進行任何操作,它將只供c3p0測試 使用。Default: null--> <property name="automaticTestTable">Test</property>
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效 保留,并在下次調用getConnection()的時候繼續(xù)嘗試獲取連接。如果設為true,那么在嘗試 獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關閉。Default: false--> <property name="breakAfterAcquireFailure">false</property>
<!--當連接池用完時客戶端調用getConnection()后等待獲取新連接的時間,超時后將拋出 SQLException,如設為0則無限期等待。單位毫秒。Default: 0 --> <property name="checkoutTimeout">100</property>
<!--通過實現(xiàn)ConnectionTester或QueryConnectionTester的類來測試連接。類名需制定全路徑。 Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> <property name="connectionTesterClassName"></property>
<!--指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那么無需設置,默認null即可 Default: null--> <property name="factoryClassLocation">null</property>
<!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. (文檔原文)作者強烈建議不使用的一個屬性--> <property name="forceIgnoreUnresolvedTransactions">false</property>
<!--每60秒檢查所有連接池中的空閑連接。Default: 0 --> <property name="idleConnectionTestPeriod">60</property>
<!--初始化時獲取三個連接,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize">3</property>
<!--最大空閑時間,60秒內未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime">60</property>
<!--連接池中保留的最大連接數(shù)。Default: 15 --> <property name="maxPoolSize">15</property>
<!--JDBC的標準參數(shù),用以控制數(shù)據(jù)源內加載的PreparedStatements數(shù)量。但由于預緩存的statements 屬于單個connection而不是整個連接池。所以設置這個參數(shù)需要考慮到多方面的因素。 如果maxStatements與maxStatementsPerConnection均為0,則緩存被關閉。Default: 0--> <property name="maxStatements">100</property>
<!--maxStatementsPerConnection定義了連接池內單個連接所擁有的最大緩存statements數(shù)。Default: 0 --> <property name="maxStatementsPerConnection"></property>
<!--c3p0是異步操作的,緩慢的JDBC操作通過幫助進程完成。擴展這些操作可以有效的提升性能 通過多線程實現(xiàn)多個操作同時被執(zhí)行。Default: 3--> <property name="numHelperThreads">3</property>
<!--當用戶調用getConnection()時使root用戶成為去獲取連接的用戶。主要用于連接池連接非c3p0 的數(shù)據(jù)源時。Default: null--> <property name="overrideDefaultUser">root</property>
<!--與overrideDefaultUser參數(shù)對應使用的一個參數(shù)。Default: null--> <property name="overrideDefaultPassword">password</property>
<!--密碼。Default: null--> <property name="password"></property>
<!--定義所有連接測試都執(zhí)行的測試語句。在使用連接測試的情況下這個一顯著提高測試速度。注意: 測試的表必須在初始數(shù)據(jù)源的時候就存在。Default: null--> <property name="preferredTestQuery">select id from test where id=1</property>
<!--用戶修改系統(tǒng)配置參數(shù)執(zhí)行前最多等待300秒。Default: 300 --> <property name="propertyCycle">300</property>
<!--因性能消耗大請只在需要的時候使用它。如果設為true那么在每個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提升連接測試的性能。Default: false --> <property name="testConnectionOnCheckout">false</property>
<!--如果設為true那么在取得連接的同時將校驗連接的有效性。Default: false --> <property name="testConnectionOnCheckin">true</property>
<!--用戶名。Default: null--> <property name="user">root</property>
<!--早期的c3p0版本對JDBC接口采用動態(tài)反射代理。在早期版本用途廣泛的情況下這個參數(shù) 允許用戶恢復到動態(tài)反射代理以解決不穩(wěn)定的故障。最新的非反射代理更快并且已經開始 廣泛的被使用,所以這個參數(shù)未必有用。現(xiàn)在原先的動態(tài)反射與新的非反射代理同時受到 支持,但今后可能的版本可能不支持動態(tài)反射代理。Default: false--> <property name="usesTraditionalReflectiveProxies">false</property>
<property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">25</property> <property name="minPoolSize">10</property> <property name="maxStatements">0</property> <user-overrides user="swaldman"> </user-overrides> </default-config> <named-config name="dumbTestConfig"> <property name="maxStatements">200</property> <user-overrides user="poop"> <property name="maxStatements">300</property> </user-overrides> </named-config> </c3p0-config>
|
posted @
2007-04-05 20:03 小平 閱讀(1159) |
評論 (1) |
編輯 收藏
網(wǎng)上沒有找到比較合適的,自己看了看,然后放到項目中實驗的一下。
很多人都認為比DBCP優(yōu)秀。
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driverClassName}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="initialPoolSize"><value>10</value></property>
<property name="minPoolSize"><value>5</value></property>
<property name="maxPoolSize"><value>30</value></property>
<property name="acquireIncrement"><value>5</value></property>
<property name="maxIdleTime"><value>10</value></property>
<property name="maxStatements"><value>0</value></property>
</bean>
以下幾個參數(shù)是使用的基本配置參數(shù):
initialPoolSize:
Number of Connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize
連接池初始化時獲取的鏈接數(shù),介于minPoolSize和maxPoolSize之間
minPoolSize:
Minimum number of Connections a pool will maintain at any given time.
最小鏈接數(shù)
maxPoolSize:
Maximum number of Connections a pool will maintain at any given time.
最大連接數(shù)
acquireIncrement:
Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted.
在當前連接數(shù)耗盡的時候,一次獲取的新的連接數(shù)
maxIdleTime:
Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.
最大空閑的時間,單位是秒,無用的鏈接再過時后會被回收
posted @
2007-04-05 19:57 小平 閱讀(843) |
評論 (0) |
編輯 收藏
???File file = new File(".");
??String fullPath = file.getAbsolutePath();
posted @
2007-02-26 11:33 小平 閱讀(203) |
評論 (0) |
編輯 收藏
1、編制菜單:?
?????每個Plug-In可以包括一個MenuBar(菜單)、CoolBar(工具欄)、PerspectiveBar(面板欄)、FastViewBar(快速視圖欄),這些欄目是在Plug-In運行過程中不會改變的,MenuBar(菜單)由ApplicationActionBarAdvisor類的fillMenuBar()方法定義,例:
????????????MenuManager fileMenu = new MenuManager("&File", IWorkbenchActionConstants.M_FILE);
????????????fileMenu.add(new?Separator());
????????fileMenu.add(newViewAction);
CoolBar(工具欄)由ApplicationActionBarAdvisor類的fillCoolBar()方法定義,例:
IToolBarManager toolbar = new ToolBarManager(SWT.FLAT | SWT.RIGHT);
?
toolbar.add(newViewAction);
PerspectiveBar由PlugIn.xml文件中關于Perspective的配置自動生成,F(xiàn)astViweBar由PlugIn.xml文件中關于View的配置自動生成,MenuBar和CoolBar中各個功能按鈕的action在ApplicationActionBarAdvisor類的makeActions()方法中注冊。例:
newViewAction?
=
?
new
?OpenNewViewAction(window,?
"
打開新視圖菜單
"
,?IEntryID.NEW_VIEW_ID);
????????register(newViewAction);
2、編寫面板和視圖:?
??????在Plug-In運行過程中Perspective(面板),View(視圖)會被經常切換,其中Perspective由若干個View組成,Perspective要實現(xiàn)IPerspectiveFactory接口,并在Perspective中定義要加載的View。View要繼承ViewPart類,View還要有一個唯一的ID。并把Perspective和View在PlugIn.xml作注冊。例:
<
extension???
point
="org.eclipse.ui.views"
>
<
view
????????????
name
="新打開的視圖"
????????????allowMultiple
="true"
????????????icon
="icons/sample3.gif"
????????????class
="uuu.NewView"
????????????id
="uuu.NewView"
>
??????
</
view
>
</
extension
>
???
<
extension????
point
="org.eclipse.ui.perspectives"
>
??????
<
perspective
????????????
name
="New?Perspective"
????????????class
="uuu.NewPerspective"
????????????id
="uuu.NewPerspective"
>
??????
</
perspective
>
???
</
extension
>
3、添加action
編寫一個繼承了Action的XXXaction類,再此之前先定義一個接口,如下:
public
?
interface
?IEntryID?
{

????
public
?
static
?
final
?String?NEW_VIEW_ID?
=
?
"
uuu.NewView
"
;
????
public
?
static
?
final
?String?CMD_OPEN_NEW_VIEW?
=
?
"
uuu.OpenNewView
"
;????
}
實現(xiàn)XXXaction的構造函數(shù),如下:
this
.window?
=
?window;
????????
this
.viewId?
=
?viewId;
????????setText(label);
????????
//
?The?id?is?used?to?refer?to?the?action?in?a?menu?or?toolbar
????????setId(IEntryID.CMD_OPEN_NEW_VIEW);
????????
//
?Associate?the?action?with?a?pre-defined?command,?to?allow?key?bindings.
????????setActionDefinitionId(ICommandIds.CMD_OPEN);
????????setImageDescriptor(uuu.Activator.getImageDescriptor(
"
/icons/sample2.gif
"
));
在PlugIn.xml中添加相應的配置:
<
extension??????
point
="org.eclipse.ui.commands"
>
????????
<
command
????????????
name
="打開新視圖"
????????????description
="打開一個新的視圖"
????????????categoryId
="uuu.OpenNewView"
????????????id
="uuu.OpenNewView"
>
??????
</
command
>
????
</
extension?
>
4、顯示工具欄:
在ApplicationWorkbenchWindowAdvisor類的preWindowOpen();方法中添加
?
?configurer.setShowCoolBar(
true
);
?configurer.setShowPerspectiveBar(
true
);
?configurer.setShowFastViewBars(
true
);
posted @
2007-01-31 14:33 小平 閱讀(350) |
評論 (0) |
編輯 收藏