on the detailed explanation of the connection pool problem between Tomcat and MySQL (original)
, and finally gained something. I hope it will be helpful to everyone. First of all, please note: here we discuss specifically the problems encountered in the Tomcat 5.5 version. Why this version is particularly specific, I will explain it in a moment.
Question 1: Cannot create JDBC driver of class '' for connect URL 'null'
answer:
[Cause analysis]
Many friends found that the above error occurred when calling the connection pool after configuring $Tomcat/conf/server.xml, $Tomcat/conf/context.xml, or even WEB-INF/web.xml. Analyzing the cause of the error, it is generally because we have not bound the data source (the actual cause of the error is because the settings of driverClassName and url are empty, but we will definitely not forget to set this place, so we must have completed the settings without connecting!). Usually there are two ways to configure the data source (not just the setting method in $Tomcat/conf/context.xml). One is to add the following code before </GlobalNamingResources> in $Tomcat/conf/server.xml:
Setting method one:
<Resource
name="jdbc/test" //Data source name
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" //This is the setting of driverClassName I just mentioned
password="admin" //Database password
maxIdle="2"
maxWait="5000"
username="root" //Database username
url="jdbc:mysql://localhost:3306/test?autoReconnect=true" //Database URL, which is the url just mentioned
maxActive="4"/>
Except where there are comments, all parameters are used to set the number of connections, idle status and activity status. If you are just doing a learning experiment, you don't need to change it. The effect of this method is equivalent to configuring operations in the Tomcat graphical interface.
Another way is to add the following code to <Context ...></Context> in $Tomcat/conf/server.xml:
Setting method two:
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/test">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>admin</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
</parameter>
<parameter>
<name>initialSize</name>
<value>20</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
The usual purpose of this setting is to allow the data source to be implemented in a separate mapping directory, that is, usually this code appears in <Context docBase="specific directory (such as D:webappsmyjsp)" path= "Access name (such as /myjsp)" reloadable="true"></Context>, so that you can access a file in D:webappsmyjsp by accessing http://localhost:8080/myjsp/XXX.jsp The jsp page then retrieves the data source or performs other operations to retrieve the data source.
The problem is, no matter which method, no one is connected! If you don't use ResourceLink, the bean or bean will not be able to find the code you set. How can you find the driverClassName and url? In fact, none of the parameter settings will be found!
[Solution]
Once you know the reason, the solution will be easier. No matter which method you use.
Solution one:
If you want to implement ResourceLink in a mapping directory, add <ResourceLink global="data source name" name="mapped name" type="javax.sql.DataSource" in the middle of <Context...></Context> "/>, please note that if you don't understand the code relationship, you must write it closely after <Context...>.
Solution two:
If you want to make it global and usable in all mapping directories, just write it in $Tomcat/conf/context.xml and everything will be OK.
Generally, everyone sets the "data source name" and "mapped name" to be the same. For example, I set it like this: <ResourceLink global="jdbc/test" name="jdbc/test" type="javax.sql.DataSource" "/>.
In addition, it must be noted that in Tomcat5.5, if you set up the second method above, even if you add ResourceLink, you will encounter the problem of not running properly.
Question 2: javax.naming.NameNotFoundException: Name XXX is not bound in this Context
answer:
[Cause analysis]
In Tomcat5.5, setting the data source in <Context...><Context> does not work properly. I have read a post written by a foreigner. My English level is not high, but I can just understand it. He said this is because in higher versions (should be Tomcat and dbcp), the value of factory has been changed from org.apache.commons.dbcp.BasicDataSourceFactory to org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory. But I tried it and it didn't solve the problem. And I found that the standard version of Tomcat5.5 comes with dbcp, which is in $Tomcatcommonlib. I read another netizen's explanation, and he said that the second setting method is not possible. This shows that the second setting method is only applicable to previous versions. I don’t know which part to make changes in the current version, but the reason for the error is that I want to set it up together, and the server cannot find the data source corresponding to the "data source name".
[Solution]
You can solve it if you know the reason. It seems that it only works if the data source is declared in <GlobalNamingResources>. So everyone can set up Tomcat5.5 according to the first setting method (this is why I talk about Tomcat5.5 in particular).
Question 3: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
answer:
[Cause analysis]
Not only for mysql, but also for other databases, as long as it cannot be found, this error "Cannot load JDBC driver class" will be thrown. So why can't the database driver class be found? How to find it? It's actually very simple.
[Solution]
Just copy jdbc to $Tomcatcommonlib.
Question 4: Cannot get a connection, pool exhausted
[Cause analysis]
It's very simple. The connection cannot be established and the connection pool overflows. This means that your connection resources are wasted because you did not recycle them in time.
[Solution]
Use the close() method promptly and correctly to release ResultSet, Statement, and Connection. I won’t go into the specific statements. It is recommended to write them in finally.
Summary: So, if you want to use Tomcat5.5 to establish a data source connection pool, there are only three steps.
First: Set the data source. It is recommended to use the graphical operation interface. If you do it manually, add the following code before </GlobalNamingResources> in $Tomcat/conf/server.xml:
<Resource
name="jdbc/test" //Data source name
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver" //This is the setting of driverClassName I just mentioned
password="admin" //Database password
maxIdle="2"
maxWait="5000"
username="root" //Database username
url="jdbc:mysql://localhost:3306/test?autoReconnect=true" //Database URL, which is the url just mentioned
maxActive="4"/>
comment parameters to your own.
Second: Set up Resource connection. It is recommended to write <ResourceLink global="data source name" name="mapped name" type="javax.sql.DataSource"/> in $Tomcat/conf/context.xml. If you want to map it in a separate Directory implementation is written after <Context...> in the directory that needs to be mapped in $Tomcat/conf/server.xml.
Third: Copy JDBC to $Tomcatcommonlib.
In addition, I will not explain in detail how to call the data source here. This problem is relatively simple, but please note that DataSource ds=(DataSource)envCtx.lookup("Reference data source "); The "referenced data source" in the statement is only the "mapped name", not the "data source name", so I suggest that you set the two names to be the same for convenience. And pay special attention to releasing idle resources in time, otherwise the connection pool will overflow!
The above are some results of my research today. I am still a beginner. I hope this article will be helpful to everyone. If you have any questions, please contact me. My email is: [email protected] (people who love to see neon lights). This article is original to neonlight.bokee.com (CSDN_MathMagician) and cannot be reproduced! August 11, 2006
http://blog.csdn.net/mathmagician/archive/2007/03/01/1518689.aspx