這幾天做項(xiàng)目,需要做一個(gè)類似于控制臺(tái)的界面,可以將日志回顯到一個(gè)文本區(qū)域里,幾經(jīng)查找,反復(fù)測(cè)試寫(xiě)了以下測(cè)試代碼。
主程序
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是一個(gè)class,test方法里只有幾個(gè)logger.info("aaa"),用于測(cè)試用,這東西可以去掉
new HH().test();


} catch (Exception e)
{
}

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

public static BeanFactory getFactory()
{

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

}
將appcontextclient.xml文件放到工程根目錄下,發(fā)布時(shí)將appcontextclient.xml放入到你的eclipse的安裝目錄即可(與eclipse的exe執(zhí)行文件相同的位置),如果需要可在appcontextclient.xml文件前加入相關(guān)的路徑來(lái)更改你的目錄(但是有可能在調(diào)試過(guò)程中必須要把a(bǔ)ppcontextclient.xml文件放到你的eclipse IDE的安裝目錄里,因?yàn)榇蟛糠謺r(shí)間我們的eclipse IDE是何 workspace分開(kāi)存放的,這樣我們?cè)谡{(diào)試的時(shí)候非常別扭,要到IDE里去修改配置文件)。
以上方法并不是唯一的,只是我感覺(jué)用起來(lái)比較合適的,這樣在調(diào)試時(shí)既可以保證appcontextclient.xml文件在工程目錄范圍內(nèi),有可以在發(fā)布時(shí)使文件保持在安裝目錄而不是運(yùn)行環(huán)境的workspace的臨時(shí)目錄里。
同時(shí)介紹幾個(gè)方法可以eclipse方法獲得eclipse工具的幾個(gè)重要路徑(注:"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
這是我調(diào)試插件時(shí)生成的臨時(shí)運(yùn)行環(huán)境目錄,是在調(diào)試IDE工程時(shí)生成的,就是發(fā)布時(shí)插件所屬eclipse運(yùn)行環(huán)境的WorkSpace目錄
Platform.getInstanceLocation().getURL()
file:/D:/runtime-IDE.product/
這個(gè)跟上面的一樣
Platform.getLocation()
D:/runtime-IDE.product
這個(gè)跟上面的也一樣
Platform.getLogFileLocation()
D:/runtime-IDE.product/.metadata/.log
運(yùn)行環(huán)境的日志文件路徑,發(fā)布時(shí)插件所屬eclipse運(yùn)行環(huán)境的WorkSpace目錄下的/.metadata/.log
Platform.getStateLocation(Platform.getBundle("IDE"))
D:/runtime-IDE.product/.metadata/.plugins/IDE
運(yùn)行環(huán)境中IDE的一個(gè)臨時(shí)目錄,發(fā)布時(shí)插件所屬eclipse運(yùn)行環(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工程的路徑,對(duì)應(yīng)發(fā)布時(shí)插件的jar本身,在這個(gè)目錄下的文件將全部包含在發(fā)不得jar里。
Platform.getConfigurationLocation().getURL()
file:/D:/WorkSpace/.metadata/.plugins/org.eclipse.pde.core/IDE.product/
eclipse IDE 自己建的目錄
eclipse 本身也有很多加載資源文件的方法可以在org.eclipse.core.runtime.Platform這個(gè)類里找到,具體的就請(qǐng)大家找找API吧
應(yīng)要求在此提供ibatis批量插入demo下載,謝謝大家的關(guān)注,不過(guò)對(duì)于一些人的不禮貌行為我非常痛心。由于工作地點(diǎn)不能上網(wǎng),不能上網(wǎng),不能及時(shí)回復(fù)大家的留言我深表道歉。
下載
此問(wèn)題網(wǎng)上大部分解決方法是這樣的:
1.網(wǎng)上鄰居->本地連接->屬性->internet協(xié)議(TCP/IP)->屬性->高級(jí)->wins標(biāo)簽->去掉啟用LMhosts查詢前的勾.
2.控制面版->windows防火墻->高級(jí)標(biāo)簽->本地連接設(shè)置->服務(wù)的標(biāo)簽里勾選安全Web服務(wù)器(HTTPS)即可
但是我的機(jī)器按照上面操作apache服務(wù)仍然不可以使用,于是索性將服務(wù)中的Windows Firewall/Internet Connection Sharing (ICS的服務(wù)關(guān)掉了,發(fā)現(xiàn)問(wèn)題解決了,apache可以用了。對(duì)于安全問(wèn)題可以選擇第3方的防火墻來(lái)解決。
注意:費(fèi)爾防火墻好像和apache沖突,也會(huì)導(dǎo)致WSASocket failed to open the inherited socket
今天用sun的jdk調(diào)的沒(méi)有一點(diǎn)問(wèn)題的數(shù)據(jù)抽取程序,方放到AIX上后不能用了,oralce報(bào)出向一個(gè)字段中插入了過(guò)長(zhǎng)的數(shù)據(jù),可是程序在幾天前還是沒(méi)有問(wèn)題的,后來(lái)分析可能是字符集導(dǎo)致的,因?yàn)榍皫滋煨薷倪^(guò)informix數(shù)據(jù)庫(kù)的字符集由8859-1轉(zhuǎn)為了zh_cn.gb18030-2000,但解決過(guò)程異常艱難,后來(lái)吧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>就可以了。后來(lái)猜測(cè)了一下原因:
1、oracle數(shù)據(jù)庫(kù)的jdbc驅(qū)動(dòng)是是自動(dòng)將數(shù)據(jù)庫(kù)的數(shù)據(jù)轉(zhuǎn)為unicode格式,這可能跟java的String默認(rèn)為unicode有關(guān),為了讓大家方便操作,
2、sun jdk的String在初始時(shí)用的是Unicode格式,ibm的jdk應(yīng)該也是,但是有一點(diǎn),sun的jdk在通過(guò)informix jdbc驅(qū)動(dòng)取數(shù)據(jù)時(shí)不會(huì)將數(shù)據(jù)轉(zhuǎn)成其他字符集,而是仍然使用Unicode,而IBM的則是通過(guò)CLIENT_LOCALE=zh_cn.gb18030-2000配置將數(shù)據(jù)格式轉(zhuǎn)為了zh_cn.gb18030-2000。
所以在oracle作insert的時(shí)候就會(huì)由于字符集不匹配而報(bào)錯(cuò)
前兩天遇到為org.eclipse.jface.text.TextViewer添加undo、redo 并添加Ctrl+z,與Ctrl+y功能這個(gè)問(wèn)題,搜遍了國(guó)內(nèi)網(wǎng)站,也沒(méi)有個(gè)好結(jié)果,終于多天后在一個(gè)國(guó)外的svn服務(wù)器上找到了一段代碼,解決了問(wèn)題
1、為TextViewer添加TextViewerUndoManager來(lái)管理記錄
protected TextViewer textViewer;
protected TextViewerUndoManager undoManager;
//20是保存記錄的數(shù)量。
undoManager = new TextViewerUndoManager(20);
//綁定對(duì)textViewer控件的數(shù)據(jù)進(jìn)行管理
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
}
});
到一個(gè)這東西都寫(xiě)到3了,針對(duì)上回說(shuō)到30000條數(shù)據(jù)的批量插入工作。30000條數(shù)據(jù)的批量插入在一個(gè)事務(wù)里處理固然是快,但是這只是測(cè)試環(huán)境,30000條數(shù)據(jù)在數(shù)據(jù)庫(kù)的緩存里必然對(duì)數(shù)數(shù)據(jù)庫(kù)的緩存和鎖數(shù)量都是一個(gè)大的挑戰(zhàn),固在新的程序中我們使用了分批事務(wù)提交的方式,這樣為了保持?jǐn)?shù)據(jù)的正確行就只能人為控制數(shù)據(jù)庫(kù)中已被插入的數(shù)據(jù)是否delete掉。另外,使用Batch塊提交會(huì)引發(fā)一個(gè)問(wèn)題就是,如果batch塊中發(fā)生了異常,我們得不到異常數(shù)據(jù)的行號(hào)即任何信息,所以只能是魚(yú)和熊掌不可兼得(我已關(guān)注過(guò)insert方法中返回pk的方法了,但好像在batch中他反回不了出錯(cuò)的行號(hào),也許是我沒(méi)有找到方法,如有人有好方法請(qǐng)共享一下,在這里表示感謝),大家酌情考慮吧,只能到到自己需要的平衡點(diǎn)了。
建議:如果對(duì)數(shù)據(jù)的準(zhǔn)確性毋庸置疑的話就是用batch處理。如果不能確定準(zhǔn)確性的話,如果對(duì)那條數(shù)據(jù)出錯(cuò)無(wú)所謂的話就也可以用batch,但是非要返回出錯(cuò)行號(hào)的話就不要用batch了,直接在外面套用一個(gè)事務(wù),然后try catch一下,處理一下行號(hào)。
今天發(fā)生了ORA-01461 :can bind a LONG value only for insert into a LONG ...到處查詢發(fā)現(xiàn)是一個(gè)字段中的中文內(nèi)容導(dǎo)致,仔細(xì)分析了一下原因,是因?yàn)閖dk1.5的String類型為utf-16編碼方式,而jdk1.4為utf-8,通過(guò)在oracle網(wǎng)上查詢10g的jdbc驅(qū)動(dòng)有兩個(gè)版本,舊版的不持jdk1.5,只支持jdk1.4,通過(guò)換jdbc驅(qū)動(dòng)問(wèn)題解決。
1、上回的心得中我強(qiáng)調(diào)了startBatch()的批處理的作用,但是其中的使用是個(gè)錯(cuò)誤用法,并沒(méi)有發(fā)揮出startBatch()的實(shí)力,對(duì)此給與觀眾的誤導(dǎo)我將在此表示到欠,并貼出正確的用法

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);//沒(méi)有意義只為測(cè)試
}
}
executor.executeBatch();
return null;
}
});
}

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

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();
//開(kāi)啟事務(wù)
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();// 這兩句有問(wèn)題,請(qǐng)見(jiàn)Spring+ibatis心得2!

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

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、為保證不會(huì)出現(xiàn)內(nèi)存溢出
hibernate.jdbc.batch_size 設(shè)為 20-50
并在代碼中沒(méi)隔50個(gè)insert后手工清理數(shù)據(jù)
if(num%50==0)

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

2、為保證減少二級(jí)緩存導(dǎo)致的過(guò)多的內(nèi)存開(kāi)銷關(guān),閉二級(jí)緩存
hibernate.cache.use_second_level_cache 設(shè)為false
3、保證使用長(zhǎng)事務(wù),不要在每個(gè)插入都事務(wù)提交,這樣性能可以有很大幅度的提升(由于原先配的事務(wù)沒(méi)有正常運(yùn)行,在初次測(cè)試時(shí)此程序插入4萬(wàn)條數(shù)據(jù)用了12分鐘,使用了長(zhǎng)事務(wù)后僅為34秒)
4、使用ScrollableResults(提供了數(shù)據(jù)庫(kù)的游標(biāo)特性)然后插入的效果好像要優(yōu)于分批抓取分批插入的效果,(4萬(wàn)條數(shù)據(jù),用ScrollableResult耗時(shí)29秒)但網(wǎng)上有人聲稱批量抓取插入的效果要好可能在遠(yuǎn)程數(shù)據(jù)庫(kù)的情況下批量抓取的可靠性更高一點(diǎn)的原因。這一點(diǎn)我詢問(wèn)過(guò)公司里做數(shù)據(jù)庫(kù)比較好的人,批量處理數(shù)據(jù)是要使用游標(biāo)處理的。就游標(biāo)而言分為動(dòng)態(tài)游標(biāo),靜態(tài)游標(biāo),動(dòng)態(tài)游標(biāo)慢于靜態(tài)游標(biāo),靜態(tài)游標(biāo)慢于靜態(tài)查詢,但是如果用分批抓取數(shù)據(jù)的話就涉及到數(shù)據(jù)分段截取,為保證每次分段截取時(shí)數(shù)據(jù)的正確性,應(yīng)該要預(yù)先對(duì)數(shù)據(jù)處理,所以批量抽取數(shù)據(jù)的速度可能會(huì)慢一些。以下為使用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();
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)欄:測(cè)試");
statusLine.add(statusItem);
}
將在狀態(tài)欄中顯示:“狀態(tài)欄:測(cè)試”
2、定義動(dòng)態(tài)的StatusLine文本
WorkbenchWindowAdvisor 類中的preWindowOpen()方法加入
configurer.setShowStatusLine(true);
在要調(diào)用狀態(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來(lái)調(diào)用showStatusMessage(String msg)即可。
<!-- 前置數(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ù)庫(kù)信息的實(shí)例
<c3p0-config> <default-config> <!--當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)。Default: 3 --> <property name="acquireIncrement">3</property>
<!--定義在從數(shù)據(jù)庫(kù)獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30 --> <property name="acquireRetryAttempts">30</property>
<!--兩次連接中間隔時(shí)間,單位毫秒。Default: 1000 --> <property name="acquireRetryDelay">1000</property>
<!--連接關(guān)閉時(shí)默認(rèn)將所有未提交的操作回滾。Default: false --> <property name="autoCommitOnClose">false</property>
<!--c3p0將建一張名為Test的空表,并使用其自帶的查詢語(yǔ)句進(jìn)行測(cè)試。如果定義了這個(gè)參數(shù)那么 屬性preferredTestQuery將被忽略。你不能在這張Test表上進(jìn)行任何操作,它將只供c3p0測(cè)試 使用。Default: null--> <property name="automaticTestTable">Test</property>
<!--獲取連接失敗將會(huì)引起所有等待連接池來(lái)獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效 保留,并在下次調(diào)用getConnection()的時(shí)候繼續(xù)嘗試獲取連接。如果設(shè)為true,那么在嘗試 獲取連接失敗后該數(shù)據(jù)源將申明已斷開(kāi)并永久關(guān)閉。Default: false--> <property name="breakAfterAcquireFailure">false</property>
<!--當(dāng)連接池用完時(shí)客戶端調(diào)用getConnection()后等待獲取新連接的時(shí)間,超時(shí)后將拋出 SQLException,如設(shè)為0則無(wú)限期等待。單位毫秒。Default: 0 --> <property name="checkoutTimeout">100</property>
<!--通過(guò)實(shí)現(xiàn)ConnectionTester或QueryConnectionTester的類來(lái)測(cè)試連接。類名需制定全路徑。 Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> <property name="connectionTesterClassName"></property>
<!--指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那么無(wú)需設(shè)置,默認(rèn)null即可 Default: null--> <property name="factoryClassLocation">null</property>
<!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. (文檔原文)作者強(qiáng)烈建議不使用的一個(gè)屬性--> <property name="forceIgnoreUnresolvedTransactions">false</property>
<!--每60秒檢查所有連接池中的空閑連接。Default: 0 --> <property name="idleConnectionTestPeriod">60</property>
<!--初始化時(shí)獲取三個(gè)連接,取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize">3</property>
<!--最大空閑時(shí)間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default: 0 --> <property name="maxIdleTime">60</property>
<!--連接池中保留的最大連接數(shù)。Default: 15 --> <property name="maxPoolSize">15</property>
<!--JDBC的標(biāo)準(zhǔn)參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。但由于預(yù)緩存的statements 屬于單個(gè)connection而不是整個(gè)連接池。所以設(shè)置這個(gè)參數(shù)需要考慮到多方面的因素。 如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0--> <property name="maxStatements">100</property>
<!--maxStatementsPerConnection定義了連接池內(nèi)單個(gè)連接所擁有的最大緩存statements數(shù)。Default: 0 --> <property name="maxStatementsPerConnection"></property>
<!--c3p0是異步操作的,緩慢的JDBC操作通過(guò)幫助進(jìn)程完成。擴(kuò)展這些操作可以有效的提升性能 通過(guò)多線程實(shí)現(xiàn)多個(gè)操作同時(shí)被執(zhí)行。Default: 3--> <property name="numHelperThreads">3</property>
<!--當(dāng)用戶調(diào)用getConnection()時(shí)使root用戶成為去獲取連接的用戶。主要用于連接池連接非c3p0 的數(shù)據(jù)源時(shí)。Default: null--> <property name="overrideDefaultUser">root</property>
<!--與overrideDefaultUser參數(shù)對(duì)應(yīng)使用的一個(gè)參數(shù)。Default: null--> <property name="overrideDefaultPassword">password</property>
<!--密碼。Default: null--> <property name="password"></property>
<!--定義所有連接測(cè)試都執(zhí)行的測(cè)試語(yǔ)句。在使用連接測(cè)試的情況下這個(gè)一顯著提高測(cè)試速度。注意: 測(cè)試的表必須在初始數(shù)據(jù)源的時(shí)候就存在。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>
<!--因性能消耗大請(qǐng)只在需要的時(shí)候使用它。如果設(shè)為true那么在每個(gè)connection提交的 時(shí)候都將校驗(yàn)其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來(lái)提升連接測(cè)試的性能。Default: false --> <property name="testConnectionOnCheckout">false</property>
<!--如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。Default: false --> <property name="testConnectionOnCheckin">true</property>
<!--用戶名。Default: null--> <property name="user">root</property>
<!--早期的c3p0版本對(duì)JDBC接口采用動(dòng)態(tài)反射代理。在早期版本用途廣泛的情況下這個(gè)參數(shù) 允許用戶恢復(fù)到動(dòng)態(tài)反射代理以解決不穩(wěn)定的故障。最新的非反射代理更快并且已經(jīng)開(kāi)始 廣泛的被使用,所以這個(gè)參數(shù)未必有用。現(xiàn)在原先的動(dòng)態(tài)反射與新的非反射代理同時(shí)受到 支持,但今后可能的版本可能不支持動(dòng)態(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>
|
網(wǎng)上沒(méi)有找到比較合適的,自己看了看,然后放到項(xiàng)目中實(shí)驗(yàn)的一下。
很多人都認(rèn)為比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>
以下幾個(gè)參數(shù)是使用的基本配置參數(shù):
initialPoolSize:
Number of Connections a pool will try to acquire upon startup. Should be between minPoolSize and maxPoolSize
連接池初始化時(shí)獲取的鏈接數(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.
在當(dāng)前連接數(shù)耗盡的時(shí)候,一次獲取的新的連接數(shù)
maxIdleTime:
Seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.
最大空閑的時(shí)間,單位是秒,無(wú)用的鏈接再過(guò)時(shí)后會(huì)被回收
???File file = new File(".");
??String fullPath = file.getAbsolutePath();
1、編制菜單:?
?????每個(gè)Plug-In可以包括一個(gè)MenuBar(菜單)、CoolBar(工具欄)、PerspectiveBar(面板欄)、FastViewBar(快速視圖欄),這些欄目是在Plug-In運(yùn)行過(guò)程中不會(huì)改變的,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文件中關(guān)于Perspective的配置自動(dòng)生成,F(xiàn)astViweBar由PlugIn.xml文件中關(guān)于View的配置自動(dòng)生成,MenuBar和CoolBar中各個(gè)功能按鈕的action在ApplicationActionBarAdvisor類的makeActions()方法中注冊(cè)。例:
newViewAction?
=
?
new
?OpenNewViewAction(window,?
"
打開(kāi)新視圖菜單
"
,?IEntryID.NEW_VIEW_ID);
????????register(newViewAction);
2、編寫(xiě)面板和視圖:?
??????在Plug-In運(yùn)行過(guò)程中Perspective(面板),View(視圖)會(huì)被經(jīng)常切換,其中Perspective由若干個(gè)View組成,Perspective要實(shí)現(xiàn)IPerspectiveFactory接口,并在Perspective中定義要加載的View。View要繼承ViewPart類,View還要有一個(gè)唯一的ID。并把Perspective和View在PlugIn.xml作注冊(cè)。例:
<
extension???
point
="org.eclipse.ui.views"
>
<
view
????????????
name
="新打開(kāi)的視圖"
????????????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
編寫(xiě)一個(gè)繼承了Action的XXXaction類,再此之前先定義一個(gè)接口,如下:
public
?
interface
?IEntryID?
{

????
public
?
static
?
final
?String?NEW_VIEW_ID?
=
?
"
uuu.NewView
"
;
????
public
?
static
?
final
?String?CMD_OPEN_NEW_VIEW?
=
?
"
uuu.OpenNewView
"
;????
}
實(shí)現(xiàn)XXXaction的構(gòu)造函數(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中添加相應(yīng)的配置:
<
extension??????
point
="org.eclipse.ui.commands"
>
????????
<
command
????????????
name
="打開(kāi)新視圖"
????????????description
="打開(kāi)一個(gè)新的視圖"
????????????categoryId
="uuu.OpenNewView"
????????????id
="uuu.OpenNewView"
>
??????
</
command
>
????
</
extension?
>
4、顯示工具欄:
在ApplicationWorkbenchWindowAdvisor類的preWindowOpen();方法中添加
?
?configurer.setShowCoolBar(
true
);
?configurer.setShowPerspectiveBar(
true
);
?configurer.setShowFastViewBars(
true
);
不同的中間件的JNDI在ApplcationContext.XML中的引用是不一樣的
1、tomcat:
???<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
????????<property name="jndiName"><value>
java:comp/env/jdbc/引用名</value></property>
????</bean>
2、OC4J:
????<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
????????<property name="jndiName"><value>
jdbc/引用名</value></property>
????</bean>