title: Datasource configuring in Hibernate 5, Tomcat 8
body: <p>Need some clarification and helps. Especially appreciate describing <strong>general concepts</strong> or link where they are described.</p>
  
  <p>So, on the hibernate website I have read the next one: </p>
  
  <blockquote>
    <p>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:</p>
  </blockquote>
  
  <p>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.</p>
  
  <ol>
  <li>Does configuring Datasource and binding SessionFactory to the JNDI
  is the same process? </li>
  <li>If no, for what we use DataSource and for why we need to bind SessionFactory to JNDI (in general)?</li>
  <li>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? </li>
  <li>What is <code>hibernate.jndi.url</code>? Does it is the same as hibernate.connection.url?</li>
  <li>What is <code>hibernate.connection.datasource</code>? In docs I read that it is    "datasource JNDI name", so if I understood right it can be any name?</li>
  <li>From Hibernate docs I read that setting at least one of the properties <code>hibernate.connection.datasource, hibernate.jndi.url, hibernate.jndi.class, hibernate.connection.username, hibernate.connection.password</code> makes my app use <code>javax.sql.Datasource</code> registered in JNDI. So does the next conf already configured to use DataSource?</li>
  <li>How to check that <code>DataSource</code> used and configured fine?</li>
  </ol>
  
  <p>My <strong>hibernate.cfg.xml</strong> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
  
      &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.use_sql_comments"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.format_sql"&gt;true&lt;/property&gt;
      &lt;property name="hibernate.generate_statistics"&gt;true&lt;/property&gt;
  
      &lt;!--http://stackoverflow.com/questions/2067526/hibernate-connection-pool--&gt;
  
      &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
      &lt;!--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:--&gt;
      &lt;!--hibernate.connection.datasource,hibernate.jndi.url,hibernate.jndi.class,hibernate.connection.username,hibernate.connection.password--&gt;
      &lt;!--Datasource config--&gt;
      &lt;property name="hibernate.connection.datasource"&gt;jdbc:mysql://localhost/easywordweb&lt;/property&gt;
      &lt;!--&lt;property name="hibernate.jndi.url"&gt;??????? what is it&lt;/property&gt;--&gt;
      &lt;!--/Datasource config--&gt;
  
      &lt;!--*****************************************************************--&gt;
      &lt;!--C3P0 config--&gt;
      &lt;!--Hibernate will obtain and pool connections using java.sql.DriverManager if you set the 5 following properties --&gt;
      &lt;!--hibernate.connection.driver_class,hibernate.connection.url,hibernate.connection.username,hibernate.connection.password,hibernate.connection.pool_size--&gt;
      &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
      &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost/easywordweb&lt;/property&gt;
      &lt;property name="hibernate.connection.username"&gt;username&lt;/property&gt;
      &lt;property name="hibernate.connection.password"&gt;password&lt;/property&gt;
      &lt;!--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. --&gt;
      &lt;!--&lt;property name="hibernate.connection.pool_size"&gt;140&lt;/property&gt;--&gt;
      &lt;property name="hibernate.c3p0.max_size"&gt;140&lt;/property&gt; 
      &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
      &lt;property name="hibernate.c3p0.acquire_increment"&gt;5&lt;/property&gt;
      &lt;!--max to cache--&gt;
      &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
      &lt;!--The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Hibernate default: 0--&gt;
      &lt;property name="hibernate.c3p0.timeout"&gt;21600&lt;/property&gt;
      &lt;!--for test, change futher--&gt;
      &lt;property name="hibernate.c3p0.preferredTestQuery"&gt;SELECT 1;&lt;/property&gt; 
      &lt;!--at every connection checkin to verify that the connection is valid--&gt;
      &lt;property name="hibernate.c3p0.testConnectionOnCheckout"&gt;true&lt;/property&gt;
      &lt;!--at every connection checkout to verify that the connection is valid--&gt;
      &lt;property name="hibernate.c3p0.testConnectionOnCheckin"&gt;true&lt;/property&gt;
      &lt;!--/for test, change futher--&gt;
      &lt;property name="hibernate.connection.provider_class"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/property&gt;
      &lt;!--/C3P0 config--&gt;
      &lt;!--*****************************************************************--&gt;
  
      &lt;property name="hibernate.c3p0.validate"&gt;true&lt;/property&gt;    
  
      &lt;!--c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds--&gt;
      &lt;property name="hibernate.c3p0.idle_test_period"&gt;21000&lt;/property&gt;
  
      &lt;property name="hibernate.jdbc.batch_size"&gt;20&lt;/property&gt;
      &lt;!--Number rows to be returned if no setted--&gt;
      &lt;property name="hibernate.jdbc.fetch_size"&gt;20&lt;/property&gt;
      &lt;property name="hibernate.jdbc.use_get_generated_keys"&gt;true&lt;/property&gt;
  
      &lt;property name="hibernate.temp.use_jdbc_metadata_defaults"&gt;false&lt;/property&gt;
  
      &lt;!--FIXING: Table "...".hibernate_sequence table not found.--&gt;
      &lt;property name="hibernate.id.new_generator_mappings"&gt;false&lt;/property&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><em>Thank's everyone in advance.</em></p>