Druid Database Connection Pool usage

Source: Internet
Author: User

Package Dbtest;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.preparedstatement;import         Java.sql.resultset;import Java.sql.sqlexception;public class Jdbctest {public static void main (string[] args) {         String Driver = "Com.mysql.jdbc.Driver";         String dbName = "NNM5";         String Passwrod = "OSSDB123";         String userName = "root";         String url = "jdbc:mysql://localhost:13306/" + dbName;         String sql = "Select Ircnetnodeid, Purpose_id,ircnetypeid from Rcnetnode limit 1";        Connection conn = null;        PreparedStatement PS = null;                ResultSet rs = null;             try {class.forname (driver);             JDBC Gets the connection//connection conn = drivermanager.getconnection (URL, username,passwrod);            Druid Connection Pool GET Connection conn = Druidmanager.getinstance (). getconnection ();             PS = conn.preparestatement (SQL);             rs = Ps.executequery ();    while (Rs.next ()) {             System.out.println ("Ircnetnodeid:" + rs.getint (1) + "purpose_id:" + rs.getstring (             2) + "Ircnetypeid:" + rs.getstring (3));                 }//Close Recordset if (rs! = null) {try {rs.close ();                 } catch (SQLException e) {e.printstacktrace ();                 }}//Close declaration if (PS! = null) {try {ps.close ();                 } catch (SQLException e) {e.printstacktrace (); }}//Close link object if (conn! = null) {try {Conn.clo                 SE ();                 } catch (SQLException e) {e.printstacktrace ();         }}} catch (Exception e) {e.printstacktrace ();                 }finally{//Close Recordset if (rs! = null) {try {rs.close ();                 } catch (SQLException e) {e.printstacktrace ();                 }}//Close declaration if (PS! = null) {try {ps.close ();                 } catch (SQLException e) {e.printstacktrace (); }}//Close link object if (conn! = null) {try {Conn.clo                 SE ();                 } catch (SQLException e) {e.printstacktrace (); }             }         }            }   }

Package Dbtest;import Java.sql.connection;import Java.sql.sqlexception;import Com.alibaba.druid.pool.druiddatasource;public class Druidmanager {private Druidmanager () {} private static DRUIDM    Anager Single=null;    Private Druiddatasource DataSource; Public synchronized static Druidmanager getinstance () {if (single = null) {single = new Druidman             Ager ();         Single.initpool ();    } return single; }private void Initpool () {dataSource = new Druiddatasource ();d atasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.setusername ("root");d Atasource.setpassword ("OSSDB123");d Atasource.seturl ("jdbc:mysql:// 127.0.0.1:13306/NNM5 ");d atasource.setinitialsize (5);d Atasource.setminidle (1);d atasource.setmaxactive (10);// Enable monitoring statistics function try {datasource.setfilters ("stat");} catch (SQLException e) {}//for MySQL Datasource.setpoolpreparedstatements (false);} Public Connection getconnection () {Connection Connection = null;try {Connection = DatasouRce.getconnection ();} catch (SQLException e) {}return connection;}}

Need to download Druid.jar, project address

Https://github.com/AlibabaTech/druid/wiki enabling web monitoring statistics requires that the Servlet declaration be included in Web application's XML.
  <servlet>      <servlet-name>DruidStatView</servlet-name>      <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>  </servlet>  <servlet-mapping>      <servlet-name>DruidStatView</servlet-name>      <url-pattern>/druid/*</url-pattern>  <servlet-mapping>
The configuration of spring can be accessed by Http://ip:port/druid/address:
<bean name= "DataSource" class= "Com.alibaba.druid.pool.DruidDataSource" init-method= "Init" destroy-method= "close "><property name=" url "value=" ${jdbc_url} "/><property name=" username "value=" ${jdbc_username} "/> <property name= "Password" value= "${jdbc_password}"/><!--initialize connection size--><property name= "InitialSize" Value= "0"/><!--connection Pool maximum usage connections--><property name= "maxactive" value= "/><"!--connection pool Max idle-->< Property Name= "Maxidle" value= "/><!--connection pool min idle--><property name=" Minidle "value=" 0 "/><!-- Get connection Maximum wait time--><property name= "maxwait" value= "60000"/><!--<property name= "Poolpreparedstatements" Value= "true"/> <property name= maxpoolpreparedstatementperconnectionsize "value="/>--><property Name= "Validationquery" value= "${validationquery}"/><property name= "Testonborrow" value= "false"/>< Property Name= "Testonreturn" value= "false"/><property name= "Testwhileidle" value= "trUE "/><!--configuration interval to detect the idle connection that needs to be closed, in milliseconds--><property name=" Timebetweenevictionrunsmillis "value=" 60000 "/><!--configures the minimum lifetime of a connection in the pool, in milliseconds--><property name=" Minevictableidletimemillis "value=" 25200000 "/ ><!--Open removeabandoned function--><property name= "removeabandoned" value= "true"/><!--1800 seconds, that is, 30 minutes-- ><property name= "removeabandonedtimeout" value= "1800"/><!--turn off abanded connection when output error log--><property name= "Logabandoned" value= "true"/><!--monitoring database--><!--<property name= "Filters" value= "stat"/>-->< Property Name= "Filters" value= "Mergestat"/></bean>

  

Druid Database Connection Pool usage

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.