Connection Pools
description
Transcript of Connection Pools
Ti nay khng up code v, ngi rnh ri, khng bit lm g. nh a tay vit lch my ci.Ci ny th mnh cng tm hiu c trn mng, mun chia s cho cc bm lm mock 2, nu cc bn thy hp l.Vn cp: to connection ti database.1. c nhn vo BaseDAO m cc bn to trn cty. (Ko phi tt c - v cha xem ht :D).Th mi ln c thao tc vi database th phi to mt connection mi, sau khi thc hin th cn phi close() n li, k c Statement v PreparedStatement (theo coding convention). Th vic ny l chnh xc v nghip v cn phi nh th. Nhng nhn xem n hot ng: th mi ln c phi to mi connection ti database, sau li ng li, rt tn ti nguyn. (Mc d ko cn quan tm lm ti chuyn thu gom cc object ko s dng ti, v c bn Garbagecollector lm vic.) Nhng chng ta thu gom n trc phi s hp l hn, v connect ti database cng phi sau mt gii hn thi gian th connect mi t hy nh kiu l session (on ny l theo mnh ngh).Cc bn hnh dung cho 100k client request i, database server, server s vt v th no. Theo mnh ngh th mi client request ti c thc hin vi database th s t nht mt ln to connection nu lm nh pp trn.2. gii php th 2 l:Trong BaseDAO s c mt attribute sql.Connection (static) .Mi ln getConnection th s kim tra thuc tnh ny ng cha , nu cha th tr li n, ngc li th to mi v tr li connection mi ny.Code:public class BaseDAO {public static Connection dbconnect = null;
public static Connection getConnection() throws SQLException{if(dbconnect != null){return dbconnect;} else {try {Class.forName(Constants.DATABASE_DRIVER);dbconnect = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/"+ Constants.DATABASE_NAME,Constants.DATABASE_USERNAME,Constants.DATABASE_PASSWORD);} catch (ClassNotFoundException e) {} catch (SQLException e) {}}return dbconnect;}}Ci ny l mnh code ngy u hc struts1, lm ci qun l pht bn g .Cch ny n gim thiu c vic to mi Connection hn so vi gii php 1.Nhng khi kt thc mt lnh thao tc d liu th cn phi thc hin lnh close(). -> cui cng mi khi client c request ti database th cng phi to ra connection mi Lm tn tin ca anh. Mc d lm c vic.
3. Gii php 3.Connection Pools Hiu nm na th ny: Nu cha c th to ra. Thc hin xong ri ng c ng tui li. m hy a tui vo ci b cha Pools ln sau nu c cn tui th a tui ra m la lim.
Pools y mnh dng l stack.Khi getConnection() nu stack ny rng th s to mi, nu stack ko rng th s ko to mi m ch vic pop ra m dng thi.Khi close connection th thay v vic gi close() mnh s push em n vo Stack dng cho anh khc.
y l code:
package model.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Stack;
import common.Constants;
public class DbUtils {private static Stack connectPools = null;static {connectPools = new Stack();try {Class.forName(Constants.DATABASE_DRIVER).newInstance();} catch (Exception e) {}}public static Connection getConnect() throws SQLException {Connection conn = null;if (connectPools.empty()) {conn = DriverManager.getConnection(Constants.DATABASE_URL+ Constants.DATABASE_NAME,Constants.DATABASE_USERNAME,Constants.DATABASE_PASSWORD);return conn;} else {return connectPools.pop();}}public static void closeConnect(Connection conn) {if (conn != null) {connectPools.push(conn);} else {// do not something}}}
Theo mnh tm hiu u , cng ko nh ngun. th vic dng connection pools ny ch cn stack c size = 10 th n s phc v cho khong 1k client ( c thao tc database mnh :D)Chc mi ngi s hiu . Vn ko hay, c g mi ngi gp . Hin ti c cc th vin cho vic qun l connection kiu ny, cc bn c th search connection pools xem
Many Thanks!