简体   繁体   English

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

[英]Datasource configuring in Hibernate 5, Tomcat 8

Need some clarification and helps. 需要一些澄清和帮助。 Especially appreciate describing general concepts or link where they are described. 特别感谢描述一般概念或描述它们的链接。

So, on the hibernate website I have read the next one: 所以,在hibernate网站上我读了下一篇:

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. 要在应用程序服务器中使用,您几乎应该始终配置Hibernate以从JNDI中注册的应用程序服务器javax.sql.Datasource获取连接。 You will need to set at least one of the following properties: 您需要至少设置以下属性之一:

And I have a few question because at the moment I am really confused about all of that stuff with DataSource, DataDriver, Tomcat and Hibernate in general. 我有一些问题,因为目前我对DataSource,DataDriver,Tomcat和Hibernate的所有内容感到困惑。

  1. Does configuring Datasource and binding SessionFactory to the JNDI is the same process? 将数据源和SessionFactory绑定到JNDI是否是相同的过程?
  2. If no, for what we use DataSource and for why we need to bind SessionFactory to JNDI (in general)? 如果不是,我们使用DataSource以及为什么我们需要将SessionFactory绑定到JNDI(一般情况下)?
  3. Am I understood right? 我明白了吗? If we configure DataSource in hibernate.cfg.xml file we don't need to configure it in {tomcat}/conf/server.xml or {tomcat}/conf/context.xml? 如果我们在hibernate.cfg.xml文件中配置DataSource,我们不需要在{tomcat} /conf/server.xml或{tomcat} /conf/context.xml中配置它?
  4. What is hibernate.jndi.url ? 什么是hibernate.jndi.url Does it is the same as hibernate.connection.url? 它与hibernate.connection.url相同吗?
  5. What is hibernate.connection.datasource ? 什么是hibernate.connection.datasource In docs I read that it is "datasource JNDI name", so if I understood right it can be any name? 在docs中我读到它是“datasource JNDI name”,所以如果我理解正确它可以是任何名字?
  6. From Hibernate docs I read that setting at least one of the properties hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password makes my app use javax.sql.Datasource registered in JNDI. 从Hibernate docs我读到设置至少一个属性hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password使我的应用程序使用javax.sql.Datasource在JNDI中注册。 So does the next conf already configured to use DataSource? 那么下一个conf已经配置为使用DataSource了吗?
  7. How to check that DataSource used and configured fine? 如何检查DataSource使用和配置是否正常?

My hibernate.cfg.xml file: 我的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>

Thank's everyone in advance. 提前感谢大家。

In the config you have posted you are initializing the connection pool within your application. 在您发布的配置中,您正在初始化应用程序中的连接池。

An alternative is to delegate the creation of the database pool to your app/web server and expose it as a JNDI resource. 另一种方法是将数据库池的创建委派给您的app / web服务器,并将其作为JNDI资源公开。 Your application need then only specify the name of the JNDI datasource to obtain a connection. 然后,您的应用程序只需指定JNDI数据源的名称即可获得连接。

Doing this in Tomcat is documented here: 这里记录了在Tomcat中执行此操作:

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

Your hibernate.cfg.xml then looks like: 您的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.

相关问题 使用Tomcat 8.0配置JPA(休眠) - Configuring JPA(Hibernate) with Tomcat 8.0 在上下文文件中配置Tomcat 7 JDNI数据源时出现问题 - Issue configuring Tomcat 7 JDNI datasource in Context file Tomcat 8-Maven + Spring + Hibernate-oracle数据源错误 - Tomcat 8 - Maven + Spring + Hibernate - Error with oracle datasource Tomcat 7.0 for Hibernate中的数据源JNDI配置 - Datasource JNDI configuration in Tomcat 7.0 for Hibernate Tomcat Web应用程序的DataSource,Spring和Hibernate - DataSource for Tomcat web app, Spring and Hibernate 学习资源,用于在带有MySQL的Tomcat Apache上将Hibernate配置为JPA 2.0提供程序 - Learning resource for configuring Hibernate as JPA 2.0 provider on Tomcat Apache with MySQL c3p0池不起作用(在tomcat上具有休眠和数据源) - c3p0 pooling is not working (with hibernate and a datasource, on tomcat) 如果已配置Tomcat JDBC数据源,是否需要Hibernate c3p0配置 - Is Hibernate c3p0 configuration needed if Tomcat JDBC datasource is configured Tomcat和Hibernate:HibernateException:无法确定要使用的适当数据源 - Tomcat and Hibernate: HibernateException: Unable to determine appropriate DataSource to use 带有休眠和超时的tomcat数据源,等待空闲对象异常 - tomcat datasource with hibernate and Timeout waiting for idle object exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM