Selection of database connection pool Druid

Source: Internet
Author: User
Tags connection pooling log4j

I'll talk first. Database connections

Database everyone is not unfamiliar, from the name can be seen that it is "storage of Data Warehouse", then how do we go to the "warehouse" to take things? Of course I need the keys! This is our database username, password, and then we can open the door to arbitrary access to things. This time, our connection to the database is a "database connection".

This time the problem comes, when we just need to go to fetch a thing, we take things off the door, and then pull out the key, this is completely normal process, completely no problem, but, most of the time we need a piece of the warehouse to move things out of the big truck parked outside, That's when we take something to lock the door, and then open it again and lock it again, is there anything wrong with that? In my opinion, this logic is absolutely correct, nothing wrong, that is, it will inevitably affect the efficiency of our loading! Normal people will think that we do not lock the door, and so on, when the shipment is finished, we lock the door again. This can be, but, will there be security and other problems? And do you allow others to move from your open door into the warehouse?

What if, at this time, we recruit a "warehouse manager" to help us manage? Of course it's perfect, he'll give you a "warrant" when you get into the warehouse, and then maybe check your shipment when you're out, and do a record and report analysis, and then you're done with your " Authorization Order "Give it to him, another person enters of course, it does not affect your work, you two of course can move things at the same time, the warehouse manager does not lock the door when you end."

What is a database connection pool

In fact, the above "warehouse manager" I think is our program World Database connection pool. Because he is holding a lot of tokens into the warehouse, he gives you a you can go in, come out after you give him, he can then give the next person, this time we warehouse door is always open state, save a lot of us unlock, open, close, door lock operation, completely to a professional management, Even more amazing is that he can record a series of your operations.

See concept:

The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing a database connection that is idle for more than the maximum idle time to avoid missing the database connection due to the absence of a database connection being freed. This technology can significantly improve the performance of database operations.
Excerpt from Baidu Encyclopedia

In the minds of most. Net developers, there seems to be no database connection pooling concept, only the database connection, and then used to remember close and use using , there are too many open source database connection pool in Java, and as if a blow more than a bad. I actually tell you that in. Net has helped us to implement the database connection pool, we do not need any configuration, use is, but Java is not the same, we need to choose the appropriate database connection pool. Let me tell you why Ali's Druid database connection pool was selected in the project.

database connection pool for monitoring

This is its description on GitHub, it has more than 4,000 Star, these are not important, in fact, his monitoring function is very good, for the development after the completion of our code optimization provides a lot of reference, at least at the database connection level of the elevation is obviously visible. We've tuned a half-minute query to the millisecond level, which is not all about optimizing database queries, but it's important to tune in.

There are a lot of comparison on the internet, such as C3P0, Proxool, Druid, Tomcat JDBC pool and so on, you can search the detailed comparison, I will not discuss here, I provide a connection pool c3p0, Proxool, Druid, Tomcat JDBC Pool comparison test, you can refer to. Here is the official comparison of the features:

You can find them on GitHub (Https://github.com/alibaba/druid/).

It's simple to configure.

First, download druid-1.0.9.jar the import to the project
This step you go to download on the line, address: Http://central.maven.org/maven2/com/alibaba/druid/1.0.9/druid-1.0.9.jar

Second, the applicationContext.xml file configuration database connection
This seems to be not much different from other database connection pools, except for a few parameters, I posted my configuration

<!--master configuration data Source-<BeanId="Masterdatasource"class="Com.alibaba.druid.pool.DruidDataSource"Init-method="Init"Destroy-method="Close" ><PropertyName="Driverclassname" ><Value>com.mysql.jdbc.driver</Value></Property><PropertyName="url" ><Value>jdbc:mysql://127.0.0.1:3306/springdemo?useunicode=true&characterencoding=utf-8</Value></Property><PropertyName="Username" ><Value>root</Value></Property><PropertyName="Password" ><value>123456</Value></Property><!--Configure extensions by alias, common plugins are: Filter:stat logs for monitoring statistics with FILTER:LOG4J defense SQL injection Filter:wall<PropertyName="Filters"Value="Stat,log4j"/><!--the maximum number of concurrent connections--<PropertyName="Maxactive"Value="/>"<!--initialize the number of connections--<PropertyName="InitialSize"Value="5"/><!--configuration Gets the time that the connection waits timeout--<PropertyName="Maxwait"Value="60000"/>< minimum number of idle connections!--<PropertyName="Minidle"Value="5"/>< how long!--configuration interval to detect idle connections that need to be closed, in milliseconds-and<PropertyName="Timebetweenevictionrunsmillis"Value="60000"/><!--Configure the minimum time for a connection to survive in a pool, in milliseconds--<PropertyName="Minevictableidletimemillis"Value="300000"/><PropertyName="Validationquery"Value="Select ' X '"/><PropertyName="Testwhileidle"Value="True"/><PropertyName="Testonborrow"Value="False"/><PropertyName="Testonreturn"Value="False"/><PropertyName="Poolpreparedstatements"Value="False"/><PropertyName="Maxopenpreparedstatements"Value="/>"<!--turn on the removeabandoned feature (connection leak monitoring, suspected leak before opening)-<property name=  "removeabandoned" value= "true"/> <!--1800 seconds, i.e. 30 minutes---< Property name= "Removeabandonedtimeout" value= "1800"/> <!-- Output error log when abanded connection is off-<property  Name= "logabandoned" value= "true"/ ></BEAN>       

The above parameters can be adjusted according to their own needs. For specific configuration please visit my GitHub sample Https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/applicationContext.xml

Third, web.xml file configuration monitoring platform

<!--connection pooling enable the Web monitoring statistics feature start--><Filter><Filter-name>druidwebstatfilter</Filter-name><Filter-class>com.alibaba.druid.support.http.webstatfilter</Filter-class><Init-param><Param-name>exclusions</Param-name><param-value>*.js,*mp3,*.swf,*.xls,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</Param-value></Init-param></Filter><Filter-mapping><Filter-name>druidwebstatfilter</Filter-name><url-pattern>/*</Url-pattern></Filter-mapping><Servlet><Servlet-name>druidstatview</Servlet-name><Servlet-class>com.alibaba.druid.support.http.statviewservlet</Servlet-class><Init-param><!--allow statistics to be emptied--<Param-name>resetenable</Param-name><Param-value>true</Param-value></Init-param><Init-param><!--user name--<Param-name>loginusername</Param-name><Param-value>admin</Param-value></Init-param><Init-param><!--password--<Param-name>loginpassword</param-name> < param-value>123456</param-value > </init-param></servlet><servlet-mapping> <servlet-name>druidstatview</< Span class= "Hljs-name" >servlet-name> <url-pattern>/druid /*</url-pattern></ servlet-mapping><!--connection pooling enable the Web monitoring statistics function end-->   

Add the above configuration in the file, you can access the monitoring platform after the project address druid , see the database connection and SQL statement execution time, of course, login password and so on their own configuration. Post-Login Interface:

Summarize

Of course, I'm just talking about it. Druid database connection Pool Basic configuration, more powerful features waiting for everyone to experience, in our time project, use will be more than the original c3p0 to cool, here to thank the project group of small and medium-sized students of the technology sharing, Druid The specific situation and various configurations everyone can go to Https://github.com/alibaba/druid/view, the document is more detailed.

Selection of database connection pool Druid

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.