連接先建立一些連接,并且這些連接允許共享,因此這樣就節(jié)省了每次連接的時(shí)間開銷。Mysql數(shù)據(jù)庫為例,連接池在Tomcat中的配置與使用。

成都創(chuàng)新互聯(lián)公司是一家專注于做網(wǎng)站、成都做網(wǎng)站與策劃設(shè)計(jì),洛南網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:洛南等地區(qū)。洛南做網(wǎng)站價(jià)格咨詢:18980820575
1、創(chuàng)建數(shù)據(jù)庫Student,表student
2、配置server.xml文件。Tomcat安裝目錄下conf中server.xml文件。
GlobalNamingResources
Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/student"
maxActive="3"
/
/GlobalNamingResources
name:指定連接池的名稱
type:指定連接池的類,他負(fù)責(zé)連接池的事務(wù)處理
url:指定要連接的數(shù)據(jù)庫
driverClassName:指定連接數(shù)據(jù)庫使用的驅(qū)動程序
username:數(shù)據(jù)庫用戶名
password:數(shù)據(jù)庫密碼
maxWait:指定最大建立連接等待時(shí)間,如果超過此時(shí)間將接到異常
maxIdle:指定連接池中連接的最大空閑數(shù)
maxActive:指定連接池最大連接數(shù)
3、配置web.xml文件。
web-app
resource-ref
descriptionmysql數(shù)據(jù)庫連接池配置/description
res-ref-namejdbc/DBPool/res-ref-name
res-typejavax.sql.DataSource/res-type
res-authContainer/res-auth
res-sharing-scopeShareable/res-sharing-scope
/resource-ref
/web-app
4、配置context.xml文件
與server.xml文件所在的位置相同。
Context
ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"
/
/Context
5、測試
DataSource pool = null;
Context env = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
env = (Context)new InitialContext().lookup("java:comp/env");
//檢索指定的對象,返回此上下文的一個(gè)新實(shí)例
pool = (DataSource)env.lookup("jdbc/DBPool");
//獲得數(shù)據(jù)庫連接池
if(pool==null){out.printl("找不到指定的連接池!");}
con = pool.getConnection();
st = con.createStatement();
rs = st.executeQuery("select * from student");
}catch(Exception ex){out.printl(ne.toString());}
使用org.springframework.jdbc.datasource.DriverManagerDataSource
說明:DriverManagerDataSource建立連接是只要有連接就新建一個(gè)connection,根本沒有連接池的作用。
bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
property name="driverClassName"value${jdbc.driverClassName}/value/property
property name="url"value${jdbc.url}/value/property
property name="username"value${jdbc.username}/value/property
property name="password"value${jdbc.password}/value/property
/bean
你這個(gè)應(yīng)該是以前裝過Mysql,沒卸載掉(或沒完全卸載),精簡版的那種Mysql也算。
你的端口3306被占用了,服務(wù)名稱也占用了,你換個(gè)端口和服務(wù)名稱可以繼續(xù)安裝。如果想用默認(rèn)的,先刪除之前留下來的mysql,或者更改之前mysql的端口和服務(wù)名。
這種情況一般都是安裝Mysql第一次不完全成功,然后重新安裝會發(fā)生。
分類: 電腦/網(wǎng)絡(luò) 程序設(shè)計(jì) 其他編程語言
問題描述:
JSP MYSQL 連接池的配置,高手來QQ***********在線等詳細(xì)哦
解析:
Jboss數(shù)據(jù)庫連接池
wwx 為數(shù)據(jù)庫名
UserInfor為表名
數(shù)據(jù)庫用戶名和密碼分別為:root root
參考 matrix/translation/Wiki.jsp?page=J2EEEJB_JbossJDBC
(該blog 包括幾個(gè)主要數(shù)據(jù)庫的連接)
MySQL是一個(gè)開放源代碼的數(shù)據(jù)庫,有很多開源項(xiàng)目和小型的社團(tuán)都在使用它。
一 把mysql-connector-java-3.1.11-bin.jar復(fù)制到/server/default/lib目錄下,
(需要mysql-connector-java-3.0.16-ga.zip包 將里面的jar文件取出)
二 再把/docs/examples/jca/mysql-ds.xml復(fù)制到/server/default/deploy目錄下。修改mysql-ds.xml文件,設(shè)置為 .mysql.jdbc.Driver再把設(shè)置為jdbc:mysql/,其中是數(shù)據(jù)庫主機(jī)名是數(shù)據(jù)庫名。
例:datasources
local-tx-datasource
jndi-nameMySqlDS/jndi-name
connection-urljdbc:mysqllocalhost:3306/wwx/connection-urlwwx
driver-class.mysql.jdbc.Driver/driver-class
user-nameroot/user-name
passwordroot/password
/local-tx-datasource
/datasources
注意:如果localhost不行 就用127.0.0.1 這個(gè)一定沒問題!
還有就是如果數(shù)據(jù)庫沒有密碼!那么就這樣password/password
三 然后需要設(shè)置standardjaws.xml (或 jaws.xml 注: \server\default\conf目錄下)文件的和元素:
jaws
datasourcejava:/MySqlDS/datasource
type-mappingmySql/type-mapping
。。。。。。。
/jaws
四 同樣也需要把standardjbosscmp-jdbc.xml (或 jbosscmp-jdbc.xml文件 注: \server\default\conf目錄下)的 和 元素設(shè)置為下面這樣:
jbosscmp-jdbc
defaults
datasourcejava:/MySqlDS/datasource
datasource-mappingmySql/datasource-mapping
/defaults /jbosscmp-jdbc
說明:datasource-mappingmySql/datasource-mapping在我的Jboss里面是被注釋掉的,但我把拿回來! 就是把 ----和------ 都刪掉!
五 最后再修改login-config.xml(\server\default\conf目錄下)文件來使用MySQL:
application-policy name = "MySqlDbRealm"
authentication
login-module code = ".jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required"
module-option name ="principal"wwx/module-option
module-option name ="userName"root/module-option
module-option name ="password"root/module-option
module-option name=managedConnectionFactoryName jboss.jca:service=LocalTxCM,name=MySqlDS
/module-option
/login-module
/authentication
/application-policy
說明:這段代碼就直接 copy到login-config.xml里面就ok!不知道往哪放嗎?
嘿嘿,就放在policy 下面吧!
還有就是如果數(shù)據(jù)庫沒有密碼!那么就這樣module-option name ="password"/module-option
六 修改了mysql-ds.xml, standardjaws.xml, standardjbosscmp-jdbc.xml,和 login-config.xml 文件就可以在Jboss上使用MySQL了。
七 調(diào)試代碼
在jboss-4.0.2/server/default/deploy下 建個(gè)文件夾 : test.war
里面test.jsp, 代碼如下!
%@pageimport="javax.naming.*"%
%@pageimport="javax.sql.*"%
%@pageimport="java.sql.*"%
%
try {
Context ctx = new InitialContext(); 得到初始化上下文
Object obj = ctx.lookup("java:/MySqlDS");查找連接池
DataSource ds = (DataSource) obj;轉(zhuǎn)換成DataSource
Connection connect = ds.getConnection();從連接池中得到一個(gè)連接
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from num");這是你的數(shù)據(jù)庫的表噢!
while(rs.next()){
out.println(rs.getObject("NO"));取出一個(gè)字段
}
} catch (NamingException e) {
e.printStackTrace();
} catch(SQLException e1){
e1.printStackTrace();
}
out.flush();
out.close();
%
數(shù)據(jù)庫連接池的主要操作如下: (1)建立數(shù)據(jù)庫連接池對象(服務(wù)器啟動)。 (2)按照事先指定的參數(shù)創(chuàng)建初始數(shù)量的數(shù)據(jù)庫連接(即:空閑連接數(shù))。 (3)對于一個(gè)數(shù)據(jù)庫訪問請求,直接從連接池中得到一個(gè)連接。如果數(shù)據(jù)庫連接池對象中沒有空閑
這是關(guān)于php進(jìn)階到架構(gòu)之 swoole 系列學(xué)習(xí)課程:第三節(jié):mysql連接池
學(xué)習(xí)目標(biāo) :
了解什么是mysql連接池,以及mysql使用場景。能在實(shí)際工作使用連接池(數(shù)據(jù)庫連接池,redis連接池等等)解決高并發(fā)帶來的問題。
場景 :
每秒同時(shí)1000個(gè)并發(fā),但mysql數(shù)據(jù)庫同時(shí)只支持400個(gè)連接,這樣mysql就會宕機(jī)
解決方案 :
使用連接池,這個(gè)連接池建立了300個(gè)與mysql的連接對象,這1000個(gè)并發(fā)有序地共享連接池里的300個(gè)連接。
連接池的使用不但解決了mysql在高并發(fā)情況下宕機(jī)問題,還額外提高了性能。因?yàn)楹蚼ysql建立連接,消耗較大。使用連接池只需要連接一次mysql。
永不斷開,需要程序常駐內(nèi)存,這就需要借助swoole實(shí)現(xiàn)。
數(shù)據(jù)庫連接池是程序啟動時(shí),建立足夠的數(shù)據(jù)庫連接,并將這些連接組成一個(gè)連接。由程序動態(tài)的對連接池中的連接進(jìn)行申請,使用,釋放和回補(bǔ)。
文章標(biāo)題:mysql怎么連接池 mysql連接池是什么
網(wǎng)站路徑:http://www.chinadenli.net/article36/hijpsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司、建站公司、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)