Java ConnectionPool接続しない最後にこだわった'眠り'
質問
私はウエブアプを使用するJNDIルックアップへの接続は、データベースです。
接続の動作を返しますクエリ特に問題ございません。問題は、接続を閉じません正しくはこだわったの眠り'モードによるmysql administrator).いるよ、ということでしょうか使用できなくなることnadしています。
できる人という数ポインタとして何ができるか、接続のプールが成功しました。
public class DatabaseBean {
private static final Logger logger = Logger.getLogger(DatabaseBean.class);
private Connection conn;
private PreparedStatement prepStmt;
/**
* Zero argument constructor
* Setup generic databse connection in here to avoid redundancy
* The connection details are in /META-INF/context.xml
*/
public DatabaseBean() {
try {
InitialContext initContext = new InitialContext();
DataSource ds = (DataSource) initContext.lookup("java:/comp/env/jdbc/mysite");
conn = ds.getConnection();
}
catch (SQLException SQLEx) {
logger.fatal("There was a problem with the database connection.");
logger.fatal(SQLEx);
logger.fatal(SQLEx.getCause());
}
catch (NamingException nameEx) {
logger.fatal("There was a naming exception");
logger.fatal(nameEx);
logger.fatal(nameEx.getCause());
}
}
/**
* Execute a query. Do not use for statements (update delete insert etc).
*
* @return A ResultSet of the execute query. A set of size zero if no results were returned. It is never null.
* @see #executeUpdate() for running update, insert delete etc.
*/
public ResultSet executeQuery() {
ResultSet result = null;
try {
result = prepStmt.executeQuery();
logger.debug(prepStmt.toString());
}
catch (SQLException SQLEx) {
logger.fatal("There was an error running a query");
logger.fatal(SQLEx);
}
return result;
}
中略
public void close() {
try {
prepStmt.close();
prepStmt = null;
conn.close();
conn = null;
} catch (SQLException SQLEx) {
logger.warn("There was an error closing the database connection.");
}
}
}
これは内javabeanを使用するデータベース接続します。
public LinkedList<ImportantNoticeBean> getImportantNotices() {
DatabaseBean noticesDBBean = new DatabaseBean();
LinkedList<ImportantNoticeBean> listOfNotices = new LinkedList<ImportantNoticeBean>();
try {
PreparedStatement preStmt = noticesDBBean.getConn().prepareStatement("SELECT pseudonym, message, date_to, date_from " +
"FROM importantnotices, users " +
"WHERE importantnotices.username = users.username " +
"AND NOW() >= date_from AND NOW() <= date_to;");
noticesDBBean.setPrepStmt(preStmt);
ResultSet result = noticesDBBean.executeQuery();
while (result.next()) {
ImportantNoticeBean noticeBean = new ImportantNoticeBean();
noticeBean.setAuthor(result.getString("pseudonym"));
noticeBean.setMessage(result.getString("message"));
noticeBean.setDateTo(result.getDate("date_to"));
noticeBean.setDateFrom(result.getDate("date_from"));
listOfNotices.add(noticeBean);
}
result.close();
} catch (SQLException SQLEx) {
logger.error("There was an error in ImportantNoticesBean.getImportantNotices()");
logger.error(SQLEx);
} finally {
noticesDBBean.close();
}
return listOfNotices;
}
<Context reloadable="true">
<Resource name="jdbc/mysite"
auth="Container"
type="javax.sql.DataSource"
username="user"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysite"
maxActive="10"
maxIdle="5"
maxWait="6000"
removeAbandoned="true"
logAbandoned="false"
removeAbandonedTimeout="20"
/>
</Context>
解決 4
問題は、接続を閉じません正しくはこだわったの眠りモード
これは実際は半分です。
にいたったそれぞれのアプリを定義する新しい接続のデータベースサーバ.いた全ての接続をアプリについてビバップからハードバップの接続としてではWEB.xml 設定ファイルを実行せんでした。アプリ億いについても同様とする。問題は、その 独立したプール してきているものを掴んで、サーバーの定義を制限します。このような競合状態によるものだと考えられます。だからアプリを終了したとの接続が待っていくまでのタイムアウトが過ぎながらアプリ億人のニーズの接続が拒否される資源のものアプリを終了したときにプールがあります。一度のタイムアウトされ、接続が解放され、B又はCなど)まで設定することができます。
例えば場合の制限は、10(mySQLプ制限なし)および各アプリを使用する構成となっている最大10のが20を接続します。このように、悪い状況です。
このセクションのRTFMの 接続の詳細に。.ここでは共通の投稿、痛みがありまざまなどへのリンクその他のxmlファイルは、コンテキストから).
であることの明示的な:私の接続の詳細にWEB.xml 各アプリの戦います。
他のヒント
すいように接続した場合を除き、prepStmt.close()するSQLExceptionなアライメントが可能で、大きな接続を漏らすことになります。
どのプール実装です。合しくは、こちらをご覧ください。接続にプールが必要になり、元のMySQLの接続直後のすべてのポイントは、接続プール!だからMySQL側の接続のように生きているが、アプリを使用しない。そうするだけで簡単に開催され、TC接続プールがあります。
するために実験の設定は、接続プールがあります。お願いし縮小のプール時にシステムです。または、いいリフレッシュすべての接続ます。または、厳しい上限の数の同時接続をくりからMySQL。
の一つをクリックしてくださのコードは接続の漏れはds.getConnection()常に新しい物理的な接続コネチカット州.close()をリリースに接続した場合は、接続プールの設定方).その場合の接続MySQL側ることができるかもしれませ出場合のコードは本当に接続漏洩し又はいません。
これは同様の問題- 接続プールの設定Tomcat
これが私の回答、その質問で固定に問題のあんなできるんですよ。
DBCPのジャカルタ-コモンズのデータベース接続プールがあります。に依存してい数のジャカルタ-コモンズ部品
* Jakarta-Commons DBCP
* Jakarta-Commons Collections
* Jakarta-Commons Pool
私が使っているものと同様の接続をプールしたものの、私は設定はこれらの特性を防ぐに同じことをすでに設定されていないよtomcat.でも最初のものになってみます。
testWhileIdle=true
timeBetweenEvictionRunsMillis=300000
Okいこと全部エクスペディアで。変更したデータベースのconfig資源の
*SNIP*
maxActive="10"
maxIdle="5"
maxWait="7000"
removeAbandoned="true"
logAbandoned="false"
removeAbandonedTimeout="3"
*SNIP*
この作品もあります。何が起きてafaik、私達に接続しTomcatにはチェックを捨てながらおくつろぎのアイドル時間>3).ではこのバッチジョブ時に最大接続できます。の可能性の問題はまることができる機能を設置します10の質問を同時走行ないならしました。重要なのはremoveAbandonedTimeoutは以下のmaxWait.
これは起こっているか?ieではこのようにプールすべきであろう。の場合はのようで、少なくとも私にとっては、うまで待も(接続)が壊れる前に固定していないというよりも、ちで"打ち破り"。かいまだに少なくなってきているのです。
こ@binilに惜しまない、結果セットの場合は例外です。によってドライバの実装でこの接続である。に移動します。close()の呼び出します。
私は同じ構成をしています。場合にはmysql管理者に(windows)に示すスリープモードでだけるプレーが使用していません。こういった走行試験プログラムプログラムは複数のスレッド作りのランダムクエリをイナリを使用しています。ますこの構成:
defaultAutoCommit="false"
defaultTransactionIsolation="REPEATABLE_READ"
auth="Container"
type="javax.sql.DataSource"
logAbandoned="true"
removeAbandoned="true"
removeAbandonedTimeout="300"
maxActive="-1"
initialSize="15"
maxIdle="10"
maxWait="10000"
username="youruser"
password="youruserpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://yourhost/yourdatabase"/>