繁体   English   中英

在Hibernate 5,Tomcat 8中配置数据源

[英]Datasource configuring in Hibernate 5, Tomcat 8

需要一些澄清和帮助。 特别感谢描述一般概念或描述它们的链接。

所以,在hibernate网站上我读了下一篇:

要在应用程序服务器中使用,您几乎应该始终配置Hibernate以从JNDI中注册的应用程序服务器javax.sql.Datasource获取连接。 您需要至少设置以下属性之一:

我有一些问题,因为目前我对DataSource,DataDriver,Tomcat和Hibernate的所有内容感到困惑。

  1. 将数据源和SessionFactory绑定到JNDI是否是相同的过程?
  2. 如果不是,我们使用DataSource以及为什么我们需要将SessionFactory绑定到JNDI(一般情况下)?
  3. 我明白了吗? 如果我们在hibernate.cfg.xml文件中配置DataSource,我们不需要在{tomcat} /conf/server.xml或{tomcat} /conf/context.xml中配置它?
  4. 什么是hibernate.jndi.url 它与hibernate.connection.url相同吗?
  5. 什么是hibernate.connection.datasource 在docs中我读到它是“datasource JNDI name”,所以如果我理解正确它可以是任何名字?
  6. 从Hibernate docs我读到设置至少一个属性hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password使我的应用程序使用javax.sql.Datasource在JNDI中注册。 那么下一个conf已经配置为使用DataSource了吗?
  7. 如何检查DataSource使用和配置是否正常?

我的hibernate.cfg.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.generate_statistics">true</property>

    <!--http://stackoverflow.com/questions/2067526/hibernate-connection-pool-->

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <!--For use inside an application server, you should almost always configure Hibernate to obtain connections from an application server javax.sql.Datasource registered in JNDI. You will need to set at least one of the following properties:-->
    <!--hibernate.connection.datasource,hibernate.jndi.url,hibernate.jndi.class,hibernate.connection.username,hibernate.connection.password-->
    <!--Datasource config-->
    <property name="hibernate.connection.datasource">jdbc:mysql://localhost/easywordweb</property>
    <!--<property name="hibernate.jndi.url">??????? what is it</property>-->
    <!--/Datasource config-->

    <!--*****************************************************************-->
    <!--C3P0 config-->
    <!--Hibernate will obtain and pool connections using java.sql.DriverManager if you set the 5 following properties -->
    <!--hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password,hibernate.connection.pool_size-->
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/easywordweb</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <!--We can use a third party pool for best performance and stability, for example c3p0. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0. -->
    <!--<property name="hibernate.connection.pool_size">140</property>-->
    <property name="hibernate.c3p0.max_size">140</property> 
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.acquire_increment">5</property>
    <!--max to cache-->
    <property name="hibernate.c3p0.max_statements">50</property>
    <!--The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Hibernate default: 0-->
    <property name="hibernate.c3p0.timeout">21600</property>
    <!--for test, change futher-->
    <property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property> 
    <!--at every connection checkin to verify that the connection is valid-->
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <!--at every connection checkout to verify that the connection is valid-->
    <property name="hibernate.c3p0.testConnectionOnCheckin">true</property>
    <!--/for test, change futher-->
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <!--/C3P0 config-->
    <!--*****************************************************************-->

    <property name="hibernate.c3p0.validate">true</property>    

    <!--c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds-->
    <property name="hibernate.c3p0.idle_test_period">21000</property>

    <property name="hibernate.jdbc.batch_size">20</property>
    <!--Number rows to be returned if no setted-->
    <property name="hibernate.jdbc.fetch_size">20</property>
    <property name="hibernate.jdbc.use_get_generated_keys">true</property>

    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

    <!--FIXING: Table "...".hibernate_sequence table not found.-->
    <property name="hibernate.id.new_generator_mappings">false</property>
    </session-factory>
</hibernate-configuration>

提前感谢大家。

在您发布的配置中,您正在初始化应用程序中的连接池。

另一种方法是将数据库池的创建委派给您的app / web服务器,并将其作为JNDI资源公开。 然后,您的应用程序只需指定JNDI数据源的名称即可获得连接。

这里记录了在Tomcat中执行此操作:

https://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

您的hibernate.cfg.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_sql_comments">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.generate_statistics">true</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- The Server configured JNDI datasource -->
        <property name="hibernate.connection.datasource">java:comp/env/jdbc/MyLocalDB</property>

        <property name="hibernate.jdbc.batch_size">20</property>
        <!--Number rows to be returned if no setted-->
        <property name="hibernate.jdbc.fetch_size">20</property>
        <property name="hibernate.jdbc.use_get_generated_keys">true</property>

        <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

        <!--FIXING: Table "...".hibernate_sequence table not found.-->
        <property name="hibernate.id.new_generator_mappings">false</property>
    </session-factory>
</hibernate-configuration>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM