部落客廣告聯播
2011年9月20日 星期二
2008年10月24日 星期五
WebSphere 6.1 內配置的Connection Pool DataSource取得的連線 resultSet closed錯誤....
com.ibm.db2.jcc.c.SqlException: [ibm][db2][jcc][10120][10898] 作業無效:result set 已關閉。 (result set closed)
錯誤訊息, 這些錯誤在Tomcat內使用tomcat自帶的Connection pool DataSource並不會發生。
解決方法: 使用WAS管理主控台, 設置該DataSource的參數 resultSetHoldability 為 1
(詳細設定步驟 請參考網頁:
http://gocom.primeton.com/blog_24523.htm?PHPSESSID=8...
並以「result set closed」為關鍵字搜尋該頁面)
2008年7月15日 星期二
2008年6月7日 星期六
tomcat DBCP 與 MySQL
故事是這樣的:
我的程式直接使用MySQL connector/J JDBC Driver連接資料庫操作都很正常 ,
但透過Tomcat配置的data source(DBCP)取得connection來對資料庫操作 ,
總是不定時的會出現 'No operation allowed after connection closed' 錯誤訊息,
意思是connection已經被關閉掉了,
也換了最新的JDBC Driver, 也換過Tomcat 版本,怎麼試結果都一樣,還是錯,
查了許久也作了trace, 這connection並不是我的程式關掉的,
同時還將程式移到了websphere 並使用websphere中的connection pool, 結果是不會發生這樣的錯誤的。
那麼剩下的兩個可能便是:
- DBCP程式關掉的
- MySQL server端關掉的
OK, 網上拜神(Google), 搜到許多的線索, 有人說connection url要有autoReconnect=true選項,但這僅適用於連線時間超過八小時者。bug database有類似問提,但早就已經修正好了。 也有人說要改MySQL server的my.ini (my.cnf) ,恩...改了可是還是錯。
OK...上面都是廢言....
解決方案是在 tomcat server.xml中的<context>其內的<resource>標籤(設定DataSource用的)要多一個 屬性 validationQuery="select 1" , 之後便不會再三不五時拿到被關掉的connection了,測底解決。
----------------------------------------------------------------------------------------------
為何如此能解決呢??
我猜想大概是多了validationQuery後, DBCP在做相關資料庫操作時,
若該connection沒被客戶端程式(也就是我的程式)手動關閉connection ,也沒被DBCP關閉connection ,
會先用validationQuery的sql檢查, 是否連線被MySQL server端中斷了 ,如果是就自動再從MySQL server取得一個新的connection。
以上僅是小弟猜測, 因為對DBCP內部運作並不是很熟悉 ,所以如有人能回答請不吝提供正確答案...
-------------------------------------------------------------------------------------------
但... 問題來了....
如果以上我的猜測是正確的..... 那麼DBCP自動從MySQL server再次取得新的connection , 那麼也就代表了我之前被MySQL server端無端close掉的connection若是transaction的(autoCommit=false)不就無端的被中斷掉了,而且我也不會得知..... +.+ \\\
,這樣實在太太太不合理的.....................
所以說呢無端斷線問題暫時解決, 但關於transaction的疑問, 不知有哪位先進可以回答呢??
2008年5月13日 星期二
2008年2月18日 星期一
JDBC insert CLOB欄位
String s="String content字串";
StringReader sr=new StringReader(s);
3. prepare statement
Connection con = ds.getConnection();
String sql="insert testTable(colClob) values (?) ";
PreparedStatement pstmt=con.prepareStatement(sql);
2. 將該Reader寫入暫存檔
tmp=File.createTempFile("tmpInsertClob_", null);
BufferedWriter writer=new BufferedWriter(new FileWriter(tmp));
BufferedReader reader=new BufferedReader((Reader)insertVals.get(i));
//StringWriter用來計算字數用
StringWriter strW=new StringWriter();
int contentLength=0;
int data=0;
while((data=reader.read())!=-1)
{
writer.write(data);
strW.write(data);
}
strW.close();
contentLength=strW.toString().length();
strW=null;
writer.close();
reader.close();
BufferedReader reader2=new BufferedReader(new FileReader(tmp));
pstmt.setCharacterStream(i+1,reader2,contentLength);
3. 執行
pstmt.executeUpdate();
4.關閉來源串流
reader2.close();
2008年2月4日 星期一
2007年11月28日 星期三
2007年8月8日 星期三
Tomcat JDBC DataSource組態注意
但關於Connection Pool設定若是不當,輕則效能不彰、重則導致無法取得資料庫連線而造成程式錯誤。
下面是Tomcat 官方文件關於在server.xml設定DataSource的範例:
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<!-- maxActive: Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<!-- username and password: MySQL dB username and password for dB connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</context>
其中注意兩個參數:
- maxActive: 代表Pool可容納Connection的最大數量
- maxIdlel:代表閒置時保留在Pool的Connection數量
親身經歷的事--在連線數使用超過maxActive時,後面取得Connection都為null,導致無法取得資料,造成程式錯誤!
2007年5月21日 星期一
筆記:簡單五步使用JDBC RowSet (Notes:Easily using JDBC RowSet with 5 steps)
2. .setDataSourceName()
3. .setCommand() //specify SQL command here
4. tuserRowSet.setTableName()
5. query
