Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

(16) Hibernate support for connection pooling

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Unless we treat a person with love, we can't understand him.

We never can have a true view of man unless we have a love for him.

1. Connection pool knowledge

The role of connection pool: manage connections; improve the utilization efficiency of connections!

Common connection Pool: C3P0 connection Pool

Hibernate also comes with a connection pool and supports C3P0 connection pool!

Hibernate comes with connection pooling: only one connection is maintained, which is relatively crude.

Can view hibernate.properties file (% hibernate%/project/etc/hibernate.properties)

The core class that Hibernate supports for C3P0 connection pooling is org.hibernate.connection.C3P0ConnectionProvider

Tell Hibernate which connection pooling technology is being used.

# hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

Detailed configuration of connection pool in hibernate.properties:

# # Hibernate Connection Pool # # hibernate.connection.pool_size 1 # Hibernate comes with connection pool: there is only one connection # # C3P0 Connection Pool### # Hibernate supports C3P0 connection pooling # # hibernate.c3p0.max_size 2 # maximum connections # hibernate.c3p0.min_size 2 # minimum connections # hibernate.c3p0.timeout 5000 # timeout # hibernate.c3p0.max_statements 100 # maximum number of commands executed # hibernate.c3p0.idle_test_period 3000 # idle test time # hibernate.c3p0.acquire_increment 2 # when the connection is insufficient Number of connections per increase # hibernate.c3p0.validate false # # Plugin ConnectionProvider # # use a custom ConnectionProvider (if not set Hibernate will choose a built-in ConnectionProvider using hueristics) # hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider#hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider#hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider#hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider

2. Steps to use connection pooling

(1) add the jar package of C3P0

% hibernate%/lib/optional/c3p0/c3p0-0.9.1.jar

(2) add database connection pool configuration in hibernate.cfg.xml file

Core configuration

Org.hibernate.connection.C3P0ConnectionProvider 4 8 5000 10 10000 2

Complete configuration

Com.mysql.jdbc.Driver jdbc:mysql:///test root root org.hibernate.dialect.MySQL5Dialect true false update Org.hibernate.connection.C3P0ConnectionProvider 4 8 5000 10 10000 2

(3) use SHOW PROCESSLIST; for testing

Test code

@ Test public void testPool () {Session session = sf.openSession (); session.beginTransaction (); Department dept = (Department) session.get (Department.class, 3); System.out.println (dept); / / break point here and use SHOW PROCESSLIST View the active connection session.getTransaction (). Commit (); session.close ();}

Testing method

A) use SHOW PROCESSLIST; to check the number of active connections before executing the testPool () method

B) Debug and execute the testPool () method to check the number of active connections when you stop at the breakpoint

C) after the testPool () method is executed, check the number of active connections

D) modify the number of hibernate.c3p0.min_size in the configuration, and execute a _ b _ c again to check the number of active connections.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report