Hibernate JDBC connection

Source: Internet
Author: User
Tags postgresql

Usually you wantorg.hibernate.SessionFactoryTo 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.DriverManagerTo 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_sizeYou 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.libDirectory. If you sethibernate.c3p0.*Hibernate will useC3P0ConnectionProviderTo Cache
JDBC connection. If you prefer proxool, referhibernate.propertiesGo to the hibernate website to obtain more information.

This is a tool that uses c3p0.hibernate.propertiesSample 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 hibernateDatasourceYou 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 JNDIInitialContextFactoryClass(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.propertiesSample 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.charSetTo specify the charset connection attribute.

Implementationorg.hibernate.connection.ConnectionProviderInterface, you can define your own plug-in policy for obtaining JDBC connections. Sethibernate.connection.provider_classYou can select a custom implementation.

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.