2016-09-01 7 views
4

説明が必要です。具体的には、一般的な概念または記載されているリンクについて説明してください。Hibernate 5、Tomcat 8でのデータソースの設定

だから、私は次のものを読んだことがあるHibernateのウェブサイト上:

アプリケーションサーバ内で使用するために、あなたはほとんど常に configureは、アプリケーションサーバ のjavax.sql.DataSourceからの接続を取得する必要があります休止状態JNDIに登録されています。

そして、現時点では私はデータソースとその原料のすべてについて本当に混乱していますので、私はいくつかの質問があり、データドライバ、Tomcatと一般的に休止状態:あなたは、以下の特性のうちの少なくとも 1を設定する必要があります。

  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のドキュメントから、プロパティーhibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.passwordの少なくとも1つを設定すると、私のアプリケーションでJNDIにjavax.sql.Datasourceが登録されています。次の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

あなたが投稿した設定では、アプリケーション内の接続プールを初期化しています。

代わりに、データベースプールの作成を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> 
+0

こんにちはアラン、あなたの答えに感謝。 私は正しく理解しましたか?私の場合、HibernateとTomcatは絶対に別の接続プールを持っていますか?Hibernateは独自の接続を使用していましたが、Tomcat接続はまったく使用されませんでしたか? – Nickolas

+0

c3poライブラリを使用して**アプリケーション内で接続プール**を設定しました。 –

+0

ありがとうと申し訳ありません)。初めてあなたを誤解しました。 – Nickolas

関連する問題