Get the App
SLTechnology News&Howtos  ›  Database  › 

(16) Hibernate support for connection pooling

Shulou Source: shulou.com Published: 2022-06-01 04:15:30 09月20日 Update

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.

Tags: Configuration quantity method test support maximum file time core minimum crude insufficient individual number code function again only command commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS MariaDB vpn Docker