mysql資料庫連接池的配置
Ⅰ eclipse+mysql5.1+tomcat6.0 要如何配置數據連接池呢
首先連接池分為好幾種,常見的有三種
1)dbcp連接池
2)c3p0連接池
3) proxool連接池
我就說說dbcp連接池吧
在tomcat conf 的service.xml種配置:
<Resource
name="jdbc/web"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
maxActive="100"
maxIdle="30"
maxwait="5000"
username="資料庫de用戶名"
password="資料庫de密碼"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/資料庫名"
/>
注意上面的factory默認的使用tomcat.dbcp.
還可以使用commons.dbcp,具體的類在org.apache.commons.dbcp.BasicDataSource
Ⅱ jboss里怎麼配置mysql資料庫的連接池
一、要在JBoss中使用MySQL的話首先要把MySQL的JDBC驅動放到CLASSPATH中。然後再JBoss配置。
二、再把/docs/examples/jca/mysql-ds.xml復制到/server/default/deploy目錄下。修改mysql-ds.xml文件,其中是資料庫主機名是資料庫名。
我的mysql-ds.xml如下
Ⅲ c#連接mysql數據,設置資料庫連接池
一般情況下不需要你設置連接池的,如果數據頻率很高,那就需要用多線程之類的解決了,也就是數據採集後放入一個緩沖區(比如放在Queue里),然後另外一個線程持續的從這個Queue中取得數據(比如一次取一千條),然後批量提交進資料庫,這也是普通的生產和消費者關系。
Ⅳ JSP MYSQL 連接池的配置,高手來QQ185259693在線等
Jboss資料庫連接池
wwx 為資料庫名
UserInfor為表名
資料庫用戶名和密碼分別為:root root
參考 http://www.matrix.org.cn/translation/Wiki.jsp?page=J2EEEJB_JbossJDBC
(該blog 包括幾個主要資料庫的連接)
MySQL是一個開放源代碼的資料庫,有很多開源項目和小型的社團都在使用它。
一 把mysql-connector-java-3.1.11-bin.jar復制到/server/default/lib目錄下,
(需要mysql-connector-java-3.0.16-ga.zip包 將裡面的jar文件取出)
二 再把/docs/examples/jca/mysql-ds.xml復制到/server/default/deploy目錄下。修改mysql-ds.xml文件,設置為 com.mysql.jdbc.Driver再把設置為jdbc:mysql:///,其中是資料庫主機名是資料庫名。
例:<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/wwx</connection-url>//wwx
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
</local-tx-datasource>
</datasources>
注意:如果localhost不行 就用127.0.0.1 這個一定沒問題!
還有就是如果資料庫沒有密碼!那麼就這樣<password></password>
三 然後需要設置standardjaws.xml (或 jaws.xml 註: \server\default\conf目錄下)文件的和元素:
<jaws>
<datasource>java:/MySqlDS</datasource>
<type-mapping>mySql</type-mapping>
。。。。。。。
</jaws>
四 同樣也需要把standardjbosscmp-jdbc.xml (或 jbosscmp-jdbc.xml文件 注: \server\default\conf目錄下)的 和 元素設置為下面這樣:
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySql</datasource-mapping>
</defaults> </jbosscmp-jdbc>
說明:<datasource-mapping>mySql</datasource-mapping>在我的Jboss裡面是被注釋掉的,但我把拿回來! 就是把 <----和------ 都刪掉!
五 最後再修改login-config.xml(\server\default\conf目錄下)文件來使用MySQL:
<application-policy name = "MySqlDbRealm">
<authentication>
<login-mole code = "org.jboss.resource.security.ConfiguredIdentityLoginMole" flag = "required">
<mole-option name ="principal">wwx</mole-option>
<mole-option name ="userName">root</mole-option>
<mole-option name ="password">root</mole-option>
<mole-option name="managedConnectionFactoryName"> jboss.jca:service=LocalTxCM,name=MySqlDS
</mole-option>
</login-mole>
</authentication>
</application-policy>
說明:這段代碼就直接 到login-config.xml裡面就ok!不知道往哪放嗎?
嘿嘿,就放在<policy> 下面吧!
還有就是如果資料庫沒有密碼!那麼就這樣<mole-option name ="password"></mole-option>
六 修改了mysql-ds.xml, standardjaws.xml, standardjbosscmp-jdbc.xml,和 login-config.xml 文件就可以在Jboss上使用MySQL了。
七 調試代碼
在jboss-4.0.2/server/default/deploy下 建個文件夾 : 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;//轉換成DataSource
Connection connect = ds.getConnection();//從連接池中得到一個連接
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from num");//這是你的資料庫的表噢!
while(rs.next()){
out.println(rs.getObject("NO"));//取出一個欄位
}
} catch (NamingException e) {
e.printStackTrace();
} catch(SQLException e1){
e1.printStackTrace();
}
out.flush();
out.close();
%>
Ⅳ MySQL與Redis資料庫連接池介紹(圖示+源碼+代碼演示)
資料庫連接池(Connection pooling)是程序啟動時建立足夠的資料庫連接,並將這些連接組成一個連接池,由程序動態地對池中的連接進行申請,使用,釋放。
簡單的說:創建資料庫連接是一個很耗時的操作,也容易對資料庫造成安全隱患。所以,在程序初始化的時候,集中創建多個資料庫連接,並把他們集中管理,供程序使用,可以保證較快的資料庫讀寫速度,還更加安全可靠。
不使用資料庫連接池
如果不使用資料庫連接池,對於每一次SQL操作,都要走一遍下面完整的流程:
1.TCP建立連接的三次握手(客戶端與 MySQL伺服器的連接基於TCP協議)
2.MySQL認證的三次我收
3.真正的SQL執行
4.MySQL的關閉
5.TCP的四次握手關閉
可以看出來,為了執行一條SQL,需要進行大量的初始化與關閉操作
使用資料庫連接池
如果使用資料庫連接池,那麼會 事先申請(初始化)好 相關的資料庫連接,然後在之後的SQL操作中會復用這些資料庫連接,操作結束之後資料庫也不會斷開連接,而是將資料庫對象放回到資料庫連接池中
資源重用:由於資料庫連接得到重用,避免了頻繁的創建、釋放連接引起的性能開銷,在減少系統消耗的基礎上,另一方面也增進了系統運行環境的平穩性(減少內存碎片以及資料庫臨時進程/線程的數量)。
更快的系統響應速度:資料庫連接池在初始化過程中,往往已經創建了若干資料庫連接置於池中備用。 此時連接的初始化工作均已完成。對於業務請求處理而言,直接利用現有可用連接,避免了從資料庫連接初始化和釋放過程的開銷,從而縮減了系統整體響應時間。
統一的連接管理,避免資料庫連接泄露:在較為完備的資料庫連接池實現中,可根據預先的連接佔用超時設定,強制收回被佔用連接。從而避免了常規資料庫連接操作中可能出現的資源泄露。
如果說你的伺服器CPU是4核i7的,連接池大小應該為((4*2)+1)=9
相關視頻推薦
90分鍾搞懂資料庫連接池技術|linux後台開發
《tcp/ip詳解卷一》: 150行代碼拉開協議棧實現的篇章
學習地址:C/C++Linux伺服器開發/後台架構師【零聲教育】-學習視頻教程-騰訊課堂
需要C/C++ Linux伺服器架構師學習資料加qun 812855908 獲取(資料包括 C/C++,Linux,golang技術,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒體,CDN,P2P,K8S,Docker,TCP/IP,協程,DPDK,ffmpeg 等),免費分享
源碼下載
下載方式:https://github.com/dongyusheng/csdn-code/tree/master/db_pool(Github中下載)
db_pool目錄下有兩個目錄,mysql_pool目錄為MySQL連接池代碼,redis_pool為redis連接池代碼
下面介紹mysql_pool
CDBConn解析
概念: 代表一個數據連接對象實例
相關成員:
m_pDBPool:該資料庫連接對象所屬的資料庫連接池
構造函數: 綁定自己所屬於哪個資料庫連接池
Init()函數: 創建資料庫連接句柄
CDBPool解析
概念:代表一個資料庫連接池
相關成員:
Init()函數:常見指定數量的資料庫實例句柄,然後添加到m_free_list中,供後面使用
GetDBConn()函數: 用於從空閑隊列中返回可以使用的資料庫連接句柄
RelDBConn()函數: 程序使用完該資料庫句柄之後,將句柄放回到空閑隊列中
測試之前,將代碼中的資料庫地址、埠、賬號密碼等改為自己的(代碼中有好幾處)
進入MySQL, 創建mysql_pool_test資料庫
進入到mysql_pool目錄下, 創建一個build目錄並進入 :
然後輸入如下的命令進行編譯
之後就會在目錄下生成如下的可執行文件
輸入如下兩條命令進行測試: 可以看到不使用資料庫連接池,整個操作耗時4秒左右;使用連接池之後,整個操作耗時2秒左右,提升了一倍
源碼下載
下面介紹redis_pool
測試
進入到redis_pool目錄下, 創建一個build目錄並進入 :
然後輸入如下的命令進行編譯
之後就會在目錄下生成如下的可執行文件
輸入如下的命令進行測試: 可以看到不使用資料庫連接池,整個操作耗時182ms;使用連接池之後,整個操作耗時21ms,提升了很多
進入redis,可以看到我們新建的key:
Ⅵ mysql 連接池配置有哪些方式
資料庫連接池的主要操作如下: (1)建立資料庫連接池對象(伺服器啟動)。 (2)按照事先指定的參數創建初始數量的資料庫連接(即:空閑連接數)。 (3)對於一個資料庫訪問請求,直接從連接池中得到一個連接。如果資料庫連接池對象中沒有空閑
Ⅶ mysql 資料庫連接池
把url改成url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=GB2312"
Ⅷ 如何設置mysql連接池呢
連接池在Tomcat中的配置與使用。 1、創建資料庫Student,表student 2、配置server.xml文件。
Ⅸ 如何在tomcat配置mysql數據連接池
eb開發中與資料庫的連接是必不可少的,而資料庫連接池技術很好的優化了動態頁與資料庫的連接,相比單個連接資料庫連接池節省了很大的資源。用一個通俗的比喻:如果一個人洗澡需花一桶水,那一百個人就要花一百桶水,太浪費了.如果都在池子里洗,洗多少個人都不怕了。
1.將MySQL的JDBC驅動復制到Tomcat安裝目錄里的lib文件夾下。驅動可以從MySQL官網上下載,為jar包。
2.將Tomcat的配置文件Context.xml做如下修改:
<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 -1 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="javade" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
</Context>
注意代碼中紅色部分:DBTest 改為自己的項目路徑;TestDB改為自己的數據源名,但是後面使用時候要與這里的配置保持一致;javauser和 javauser改為自己MySQL的用戶名密碼;url的格式依次為jdbc:mysql://{你的資料庫服務所在的IP,如果為本機就為localhost}:{你的資料庫服務埠號}/{MySQL中要使用的資料庫名稱}?autoReconnect=true 。
3.修改項目WEB-INF/web.xml 配置文件(若無,請新建),在「</web-app>」之上添加如下代碼:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
上步中若修改了數據源名此步中紅色部分請保持與上步中的一致。
4.代碼示例:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
rs = st.executeQuery(yoursql);
注意紅色部分與上兩步中的一致;yoursql處寫你的sql代碼。
通過1-3步就在Tomcat中配置好了MySQL的資料庫連接池。
Ⅹ Tomcat5.0.28配置mysql的連接池
1.在網上很多的文章都介紹在Tomcat/conf文件下的context.xml文件中添加如下的代碼:
Resource
//這是為你的連接池起一個名字,後邊在代碼中會用到
name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdel="30"
maxWait="10000"
//這個是你的mysql資料庫的用戶名和密碼
username="root"
password="*******"
driverClassName="com.mysql.jdbc.Driver"
//conn這個是你mysql中的資料庫名
url="jdbc:mysql://localhost:3306/conn"
/
其實不用在conf下修改context.xml文件。
直接在自己的項目下的Webcontent/META-INF文件夾下新建一個context.xml文件將上面的代碼拷貝到這個xml文件中就可以了。
(這里要注意的一點就是要將這個context.xml放在Webcontent/META-INF文件夾下,而不是放在Webcontent/WEB-INF文件下)
2. 將下面代碼拷貝到項目文件/Webcontent/WEB-INF文件夾下的web.xml下。注意要放在/web-app之前。
resource-ref
// DB Connections 這是隨意起的名字,沒有影響
descriptionDB Connections/description
//jdbc/mysqlds這個就是你在context.xml中設置的連接池名字
res-ref-namejdbc/mysqlds/res-ref-name
res-typejavax.sql.DataSource/res-type
res-authContainer/res-auth
/resource-ref
3.將mysql-connector-java-5.1.6-bin.jar驅動程序拷貝到項目文件/Webcontent/WEB-INF/lib文件夾下面,同時也要放在Tomcat/lib文件夾下面。
這樣就完成了通過連接池的方式連接資料庫了。
可以通過以下代碼就行測試:
新建一個servlet文件
Conntslt.java
package com.dbconn;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class ConnPslt extends HttpServlet {
private static final long serialVersionUID = 1L;
public ConnPslt() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String title=request.getParameter("title");
String content=request.getParameter("content");
System.out.println(title);
DataSource ds = null;
try {
Context context=new InitialContext();
ds=(DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (NamingException e) {
System.out.println(e);
}
try {
Connection comm=ds.getConnection();
String sql="insert into webblog (title,content)values(?,?)";
PreparedStatement pstmt=comm.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, content);
int rs=pstmt.executeUpdate();
System.out.println(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
再建一個jsp文件
index.jsp
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN ;
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
titleInsert title here/title
/head
body
form name="form1" method="post" action="/DBconn/ConnPslt"
table width="320" border="1" align="center"
tr
td colspan="2" align="center"留言板/td
/tr
tr
td width="84"標題/td
td width="220"label
input type="text" name="title" width="200"
/label/td
/tr
tr
td height="73"內容/td
tdlabel
textarea name="content" cols="30" rows="10"/textarea
/label/td
/tr
tr
td colspan="2" align="center"label
input type="submit" name="Submit" value="提交"
/label/td
/tr
/table
/form
/body
/html
在控制台輸出的結果如果是1,則添加成功,如果是0則說明資料庫連接失敗,要自己找找問題,