2016-09-01 76 views
4

需要一些說明和幫助。特別感謝描述一般概念或鏈接描述它們的地方。休眠中的數據源配置5,Tomcat 8

所以,Hibernate網站上我看了下一個:

對於應用程序服務器內使用,你應該總是 配置Hibernate從應用服務器 javax.sql.Datasource取得獲取連接在JNDI中註冊。您需要設置下列屬性中的至少 一個:

而且我有幾個問題,因爲在那一刻,我真搞不清楚所有與數據源,DataDriver下,Tomcat的東西和一般的休眠。

  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?在文檔中,我讀到它是「數據源JNDI名稱」,所以如果我理解正確,它可以是任何名稱?
  6. 從休眠文檔我讀了設置至少有一個屬性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> 

感謝的提前每一個人。

回答

2

在您發佈的配置中,您正在初始化應用程序中的連接池。

另一種方法是將創建數據庫池委派給您的應用程序/ 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> 
+0

艾倫您好,感謝您的回答。 我理解對嗎?就我而言,Hibernate和Tomcat具有完全獨立的連接池?Hibernate只使用自己的連接,Tomcat連接完全不用? – Nickolas

+0

您已使用c3po庫在**應用程序中配置了連接池**。 –

+0

謝謝,我很抱歉)。第一次誤解了你。 – Nickolas