FAQs about MySQL connection pool and Tomcat

Source: Internet
Author: User
Tags mysql connection pool

The following article mainly describes how to resolve issues related to tomcat (a very useful JSP running platform) and MySQL connection pool, the materials I saw on a website with good reputation two days ago are very good and I will share them with you.

After studying for a day, I finally learned something and hope to help you. First of all, please note: here we will discuss in particular the problems encountered in tomcat (a very useful JSP running platform) Version 5.5. Why is this version especially specific? I will explain it later.

Problem 1: Cannot create JDBC driver of class ''for connect URL 'null'

A:

[Cause Analysis]

Many friends are configuring $ tomcat (a very useful JSP running platform)/conf/server. xml (standardization is getting closer and closer), $ tomcat (a very useful JSP running platform)/conf/context. xml (standardization is getting closer and closer), and even WEB-INF/web. xml (standardization is getting closer and closer), and the above error occurs when MySQL connection pool is called. The cause of the error is analyzed. This is generally because you do not specify a data source (the actual cause is that driverClassName and url are set to null, but you will certainly not forget to set this field, so it must have been set up and no connection is available !). Generally, you can configure the data source in $ tomcat (a very useful JSP running platform)/conf/context. the setting method of xml (standardization is getting closer and closer) is not enough. One is in $ tomcat (a very useful JSP running platform)/conf/server. add the following code before </GlobalNamingResources> of xml (standardization is getting closer and closer:

Method 1:

<Resource

Name = "jdbc/test" // data source name

Type = "javax. SQL. DataSource"

DriverClassName = "com. MySQL (the best combination with PHP). 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 (the best combination with PHP): // localhost: 3306/test? AutoReconnect = true "// Database URL, which is the url just mentioned

MaxActive = "4"/>

In addition to comments, all parameters are set for the number of connections, idle status, and activity status. If you are only doing a learning test, you do not have to change it. This method is equivalent to configuring operations in the tomcat (a good JSP running platform) graphical interface.

Another method is in $ tomcat (a very useful JSP running platform)/conf/server. <Context...> </Context> Add the following code:

Method 2:

 
 
  1. <Resource name = "jdbc/test" auth = "Container" type = "javax. SQL. DataSource"/>
  2. <ResourceParams name = "jdbc/test">
  3. <Parameter>
  4. <Name> factory </name>
  5. <Value> org. apache (the most popular WEB server platform on the Unix platform). commons. dbcp. BasicDataSourceFactory </value>
  6. </Parameter>
  7. <Parameter>
  8. <Name> username </name>
  9. <Value> root </value>
  10. </Parameter>
  11. <Parameter>
  12. <Name> password </name>
  13. <Value> admin </value>
  14. </Parameter>
  15. <Parameter>
  16. <Name> driverClassName </name>
  17. <Value> com. MySQL (the best combination with PHP). jdbc. Driver </value>
  18. </Parameter>
  19. <Parameter>
  20. <Name> url </name>
  21. <Value> jdbc: MySQL (the best combination with PHP): // localhost: 3306/test? AutoReconnect = true </value>
  22. </Parameter>
  23. <Parameter>
  24. <Name> initialSize </name>
  25. <Value> 20 </value>
  26. </Parameter>
  27. <Parameter>
  28. <Name> maxActive </name>
  29. <Value> 30 </value>
  30. </Parameter>
  31. <Parameter>
  32. <Name> maxWait </name>
  33. <Value> 10000 </value>
  34. </Parameter>
  35. </ResourceParams>

The general purpose of this setting is to make the data source be implemented in a separate ing directory, that is, the Code usually appears in the <Context docBase = "specific directory (such as D: \ webapps \ myjsp (preferred for SUN Enterprise Applications) "path =" Access name (e.g./myjsp (preferred for SUN Enterprise Applications )) "reloadable =" true "> </Context> to access http: // localhost: 8080/myjsp (preferred for SUN Enterprise Applications)/XXX. jsp (preferred for SUN Enterprise applications) to access a jsp in D: \ webapps \ myjsp (preferred for SUN Enterprise Applications) (preferred for SUN Enterprise applications) page to retrieve the data source or perform other operations to retrieve the data source.

The problem is that no connection is established in any way! If you do not use ResourceLink or bean, you cannot find the code you set. How can you find driverClassName and url? In fact, no parameter setting will be found!

[Solution]

Knowing the cause makes it easy to solve the problem. No matter which method you use.

Solution 1:

If you want to implement ResourceLink in a ing directory </Context> Add <ResourceLink global = "Data Source name" name = "ing name" type = "javax. SQL. dataSource "/>. Note that if you do not understand the code Association, you must stick it closely to <Context...> write it later.

Solution 2:

If you want to make it global, you can use it in all the ing directories, simply write it in $ tomcat (a very useful JSP running platform)/conf/context. everything is okay in xml (standardization is getting closer and closer.

Generally, you can set "Data Source Name" and "ing name" to the same name, for example, <ResourceLink global = "jdbc/test" name = "jdbc/test" type = "javax. SQL. dataSource "/>.

In addition, it must be noted that in tomcat (a very useful JSP running platform) 5.5, if you add ResourceLink to the second method above, you will encounter problems that cannot run normally.

Question 2: javax. naming. NameNotFoundException: Name XXX is not bound in this Context

A:

[Cause Analysis]

Tomcat (a very useful JSP running platform) 5.5, in <Context...> the data source set in <Context> cannot run normally. I have read a post written by a foreigner. I am not good at English, but I can understand it. He said that this is because in higher versions (tomcat (a very useful JSP running platform) and dbcp), the factory value is from org. apache (the most popular WEB server platform on Unix ). commons. dbcp. basicDataSourceFactory is changed to org. apache (the most popular WEB server platform on Unix ). tomcat (a very useful JSP running platform ). dbcp. dbcp. basicDataSourceFactory.

However, I tried to solve the problem. In addition, I found that tomcat (a very useful JSP running platform) comes with dbcp In the 5.5 Standard version, which is in $ tomcat (a very useful JSP running platform) \ common \ lib. I have read another netizen's explanation, and he said that the second setting method would not work. This indicates that the second method only applies to previous versions. I don't know where to change the current version, but the cause of the error is that I want to set it together, the server cannot find the data source corresponding to the data source name.

[Solution]

You can solve the problem by knowing the cause. It seems that the data source declaration can only take effect in <GlobalNamingResources>. So we set tomcat (a very useful JSP running platform) 5.5 According to the first setting method (that is why I especially talked about tomcat (a very useful JSP running platform) 5.5) you can.

Question 3: Cannot load JDBC driver class 'com. MySQL (the best combination with PHP). jdbc. Driver'

A:

[Cause Analysis]

This error "Cannot load JDBC driver class" is thrown not only for MySQL (the best combination with PHP), but for other databases that Cannot be found ", why can't we find the database driver class? How can this problem be found? It is actually very simple.

[Solution]

Just copy jdbc to $ tomcat (a useful JSP running platform) \ common \ lib.

Question 4: Cannot get a connection, pool exhausted

[Cause Analysis]

It is very simple. You cannot establish a connection, and the MySQL connection pool overflows. This indicates that your connection resources are wasted because you did not recycle them in time.

[Solution]

Promptly and correctly use the close () method to release ResultSet, Statement, and Connection. I will not talk about the specific Statement. It is recommended to write it in finally.

Conclusion: If you want to use tomcat (a good JSP running platform) 5.5 to create a data source MySQL connection pool, you only need three steps.

First, we recommend that you use the graphic operation interface to set the data source. If you are manually running $ tomcat (a very useful JSP running platform)/conf/server. add the following code before </GlobalNamingResources> in xml (standardization is getting closer and closer:

 
 
  1. <Resource
  2. Name = "jdbc/test" // data source name
  3. Type = "javax. SQL. DataSource"
  4. DriverClassName = "com. MySQL (the best combination with PHP). jdbc. Driver" // This is the setting of driverClassName I just mentioned.
  5. Password = "admin" // Database password
  6. MaxIdle = "2"
  7. MaxWait = "5000"
  8. Username = "root" // database username
  9. Url = "jdbc: MySQL (the best combination with PHP): // localhost: 3306/test? AutoReconnect = true "// Database URL, which is the url just mentioned
  10. MaxActive = "4"/>

Remember to change the annotation parameter to your own.

Second, set the Resource connection. We recommend that you use $ tomcat (a very useful JSP running platform)/conf/context. <ResourceLink global = "Data Source name" name = "ing name" type = "javax. SQL. dataSource "/>, if you want to implement it in a separate ing directory, you can find $ tomcat (a very useful JSP running platform)/conf/server. <Context...>.

Third: Copy JDBC to $ tomcat (a useful JSP running platform) \ common \ lib

In addition, I will not describe how to call the data source here. This problem is simple, but note that DataSource ds = (DataSource) envCtx. lookup ("Reference Data Source"); in the statement, "Reference Data Source" only indicates "ing name", not "Data Source Name ", so I suggest you set the two names to the same one for convenience. In addition, pay special attention to releasing idle resources in time, or the MySQL connection pool will overflow!

The above are some of my research achievements today. I am still a beginner. I hope this article will help you. Can you contact me if you have any questions, my mailbox is: lk_l@sina.com (love to watch the neon ). This article is original to neonlight.bokee.com (CSDN_MathMagician,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.