Hibernate高级配置

来源:互联网 发布:库里总决赛数据 编辑:程序博客网 时间:2024/05/17 02:18

1.配置数据库连接池

1.1.使用默认的数据库连接池

hibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.connection.driver_class = com.mysql.jdbc.Driverhibernate.connection.url = jdbc:mysql://localhost:3306/sampledbhibernate.connection.username = roothibernate.connection.password = 1234hibernate.show_sql = true

1.2.使用C3P0连接池

hibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.connection.driver_class = com.mysql.jdbc.Driverhibernate.connection.url = jdbc:mysql://localhost:3306/sampledbhibernate.connection.username = roothibernate.connection.password = 1234hibernate.show_sql = truehibernate.c3p0.min_size = 5hibernate.c3p0.max_size = 20hibernate.c3p0.timeout = 300hibernate.c3p0.max_statements = 50hibernate.c3p0.idle_test_period = 3000

1.3.从容器中获得数据源

<Resource name="jdbc/SAMPLEDB"    auth="Container"    type="javax.sql.DataSource"      maxActive="100"    maxIdle="30"    maxWait="10000"    username="root"    password="1234"    driverClassName="com.mysql.jdbc.Driver"    url="jdbc:mysql://localhost:3306/SAMPLEDB?autoReconnect=true" />

hibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.connection.datasource = java:comp/env/jdbc/SAMPLEDBhibernate.show_sql = true

2.把SessionFactory与JNDI绑定

hibernate.dialect = org.hibernate.dialect.MySQLDialecthibernate.connection.datasource = java:comp/env/jdbc/SAMPLEDBhibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactoryhibernate.transaction.manager_lookup_class = org.hibernate.transaction.JBossTransactionManagerLookuphibernate.session_factory_name = java:hibernate/HibernateFactoryhibernate.show_sql = true

使用JNDI访问和JNDI绑定的SessionFactory实例:

Context ctx = new InitialContext();String jndiName = "java:hibernate/HibernateFactory";SessionFactory sessionFactory = (SessionFactory)ctx.lookup(jndiName);Session session = sessionFactory .openSession();...

0 0
原创粉丝点击