Usually you wantorg.hibernate.SessionFactory
To create a JDBC connection with the cache. If you use this method, you only need to openorg.hibernate.Session
:
Session session = sessions.openSession(); // open a new Session
Once you need to access data, a JDBC connection is obtained from the connection pool.
To make this method work, we need to pass some JDBC connection attributes to hibernate. The names and semantics of all hibernate attributes areorg.hibernate.cfg.Environment
. Now we will describe
The most important setting in JDBC connection configuration.
If you set the following attributes, Hibernate will usejava.sql.DriverManager
To obtain (and cache) JDBC connections:
Hibernate JDBC attributes
Attribute name |
Purpose |
Hibernate. Connection. driver_class |
JDBC Driver Class |
Hibernate. Connection. url |
JDBC URL |
Hibernate. Connection. Username |
Database User |
Hibernate. Connection. Password |
Database User Password |
Hibernate. Connection. pool_size |
Maximum number of pooled connections |
However, the connection pool algorithm provided by Hibernate is quite immature. It is just to help you get started quickly.And is not suitable for product systems.Or performance testing. For the best performance and stability, you should use a third-party connection pool. You only need to replace it with the settings of the specified connection pool.hibernate.connection.pool_size
You can. This will disable
The connection pool that comes with hibernate. For example, you may want to use c3p0.
C3p0 is an open source JDBC connection pool that is distributed along with hibernate.lib
Directory. If you sethibernate.c3p0.*
Hibernate will useC3P0ConnectionProvider
To Cache
JDBC connection. If you prefer proxool, referhibernate.properties
Go to the hibernate website to obtain more information.
This is a tool that uses c3p0.hibernate.properties
Sample file:
hibernate.connection.driver_class = org.postgresql.Driverhibernate.connection.url = jdbc:postgresql://localhost/mydatabasehibernate.connection.username = myuserhibernate.connection.password = secrethibernate.c3p0.min_size=5hibernate.c3p0.max_size=20hibernate.c3p0.timeout=1800hibernate.c3p0.max_statements=50hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
To use hibernate on the application server, you should always configure hibernateDatasource
You must set at least one of the following attributes:
Hibernate data source attributes
Attribute name |
Purpose |
Hibernate. Connection. datasource |
Datasource JNDI name |
Hibernate. JNDI. url |
URL of the JNDI provider(Optional) |
Hibernate. JNDI. Class |
JNDIInitialContextFactory Class(Optional) |
Hibernate. Connection. Username |
Database User(Optional) |
Hibernate. Connection. Password |
Database Password(Optional) |
This is a JNDI data source provided by the application server.hibernate.properties
Sample file:
hibernate.connection.datasource = java:/comp/env/jdbc/testhibernate.transaction.factory_class = \ org.hibernate.transaction.JTATransactionFactoryhibernate.transaction.manager_lookup_class = \ org.hibernate.transaction.JBossTransactionManagerLookuphibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
The JDBC connection obtained from the JNDI data source will be automatically involved in the container-managed transactions in the application server.
The attribute name of any connection attribute must be"hibernate.connnection
. For example, you may usehibernate.connection.charSet
To specify the charset connection attribute.
Implementationorg.hibernate.connection.ConnectionProvider
Interface, you can define your own plug-in policy for obtaining JDBC connections. Sethibernate.connection.provider_class
You can select a custom implementation.