|
前言
在許許多多的B/S架構(gòu)系統(tǒng)中都涉及到了數(shù)據(jù)庫(kù)的鏈接,那么對(duì)于數(shù)據(jù)庫(kù)連接的方式有哪些?可能出現(xiàn)的問題是什么? 目錄
1.普通連接方式
2.單例模式
3.連接池 分析
普通連接:
下面是我們一般使用的普通連接方式的代碼(jsp) package com.jdbc.dao;
import java.sql.*;
public class BaseDAO {
//打開數(shù)據(jù)庫(kù)鏈接
public Connection getConn()
{
Connection conn = null;
try {
//加載驅(qū)動(dòng)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//打開鏈接
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = epetDB","sa","sa");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//(重寫)關(guān)閉鏈接
public void Close(Connection conn,PreparedStatement pstmt,ResultSet rs)
{
try {
//關(guān)閉結(jié)果集
if (rs != null) {
rs.close();
}
//關(guān)閉PerparedStatement對(duì)象
if (pstmt != null) {
pstmt.close();
}
//關(guān)閉鏈接
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
//(重寫)關(guān)閉鏈接
public void Close(Connection conn,PreparedStatement pstmt)
{
try {
//關(guān)閉PerparedStatement對(duì)象
if (pstmt != null) {
pstmt.close();
}
//關(guān)閉鏈接
if (conn != null) {
conn.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
//增刪改操作
public int Update(String sql,Object[] parm)
{
int iRet = 0;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConn();
pstmt = conn.prepareStatement(sql);
//循環(huán)賦值參數(shù)
for (int i = 0; i < parm.length; i++) {
//為預(yù)編譯sql設(shè)置參數(shù)
pstmt.setObject(i+1, parm);
}
//執(zhí)行SQL語(yǔ)句
iRet = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
finally
{
Close(conn,pstmt);
}
return iRet;
}
}
普及: try{
//可能出現(xiàn)異常的代碼
}catch(Execption e){
//如果發(fā)生異常處理的代碼
}finally{
//無(wú)論是否異常都會(huì)執(zhí)行的代碼
try catch finally java中異常處理機(jī)制
我們來(lái)分析一下寫一段代碼,其中Update方法是用來(lái)更新數(shù)據(jù)的,其中我們可以看到try中包含了getConn()方法用來(lái)獲取Connection連接對(duì)象,到最后我們可以在finally代碼塊中看到Close()方法用來(lái)關(guān)閉創(chuàng)建的Connection對(duì)象以及PreparedStatement對(duì)象,這么消耗我們很大的內(nèi)存空間。 如果用戶同時(shí)點(diǎn)注冊(cè)按鈕那么服務(wù)器首先執(zhí)行打開數(shù)據(jù)庫(kù)連接Connection多個(gè)用戶注冊(cè)就會(huì)打開多個(gè)Connection那么并且同時(shí)添加到數(shù)據(jù)庫(kù),服務(wù)器就會(huì)在執(zhí)行添加的時(shí)候就會(huì)發(fā)生異常。分不清楚用戶注冊(cè)的信息。舉個(gè)例子:
左邊的三個(gè)人同時(shí)對(duì)另一人喊不同的一個(gè)字,右邊的一個(gè)人就會(huì)分不清,左邊三個(gè)人喊了什么?(可以做真人實(shí)例) 總結(jié):
從分析中,我們看到普通的連接方式中無(wú)法處理并發(fā)問題!如果你想知道解決方法那么請(qǐng)繼續(xù)看下去。 單例連接:
下面一段單利模式中的數(shù)據(jù)庫(kù)連接代碼 package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BaseDao {
private String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url = "jdbc:sqlserver://localhost:1433;databasename=SQLTMP";
private String user = "sa";
private String pwd = "sa";
private static Connection conn = null;
private BaseDao(){
try {
Class.forName(className);
conn = DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
if(conn != null){
return conn;
}else{
new BaseDao();
return conn;
}
}
}
普及:
構(gòu)造方法:訪問修飾符(public|private) 類名
構(gòu)造方法在實(shí)例化的時(shí)候就會(huì)調(diào)用 我們分析一下這一段代碼中Connection在構(gòu)造方法中創(chuàng)建用過getConn方法獲取連接。 我們從圖片中和代碼中可以看到全程中只有一個(gè)Connection連接,那么這樣就可以降低服務(wù)器的壓力,解決并發(fā)問題
總結(jié):
從分析中,我們看到單例模式,可以減輕服務(wù)器的壓力,解決并發(fā)問題,如果夠仔細(xì)的話大家會(huì)發(fā)現(xiàn)getConn方法是一個(gè)靜態(tài)方法,而且其他屬性和方法都是private從而大大提高了安全性。這種連接方式適合:個(gè)人開發(fā)和國(guó)家單位開發(fā)(安全性高) 連接池:
下面一段連接池?cái)?shù)據(jù)庫(kù)連接代碼 context.xml
<resource name="news" auth="Container" type="javax.sql.DataSource" maxactive="100" maxidle="30" maxwait="1000" username="sa" password="sa" driverclassname="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=NewsManagerSystem"></resource>
Web.xml
<resource-ref><description>news DataSource</description><res-ref-name>news</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>
package com.news.dao;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class BaseDao {
/**
* 創(chuàng)建連接池
* */
public Connection getConn(){
Connection conn = null;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/news");
conn = ds.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
普及:
連接池:連接池是創(chuàng)建和管理一個(gè)連接的緩沖池的技術(shù),這些連接準(zhǔn)備好被任何需要他們的線程使用。 我們可以直接使用getConn方法獲得Connection并且執(zhí)行數(shù)據(jù)操作,執(zhí)行完成之后連接池會(huì)回收Connection這樣就解決了普通模式中的并發(fā)問題. 總結(jié):
從分析中,我們看到Connection不需要?jiǎng)?chuàng)建,這樣就簡(jiǎn)化編程模式,這樣減少了連接的創(chuàng)建時(shí)間,連接池能夠使性能最大化,同事還能將資源利用控制在一定的水平之下,如果超過該水平,應(yīng)用程序?qū)⒈罎⒍粌H僅是變慢。 寫在最后
清楚的了解B/S架構(gòu)中的數(shù)據(jù)庫(kù)連接方式,在合適的情況下使用合適的連接方式感覺還是棒棒噠 ~ ~! 相關(guān)鏈接:http://bbs./thread-8784-1-1.html 不錯(cuò)的文章,分享給大家
|