Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem was happening because of small value <code>time_out</code> var on mysql server.</p> <p>In my situation time_out was set to 1 minute. Using C3PO pooling mechanism we can optimize JDBC.</p> <p>Download <strong>c3p0</strong> -> <a href="http://sourceforge.net/projects/c3p0/" rel="nofollow noreferrer">http://sourceforge.net/projects/c3p0/</a></p> <p>I'm using <strong>hibernate 3.0</strong>.</p> <p><strong>hibernate.cfg.xml</strong></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="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt; &lt;property name="connection.url"&gt;jdbc:mysql://databasehost:3306/databasename&lt;/property&gt; &lt;property name="connection.username"&gt;user&lt;/property&gt; &lt;property name="connection.password"&gt;psw&lt;/property&gt; &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt; &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt; &lt;property name="show_sql"&gt;false&lt;/property&gt; &lt;!-- hibernate c3p0 settings --&gt; &lt;property name="connection.provider_class"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/property&gt; &lt;property name="hibernate.c3p0.acquire_increment"&gt;3&lt;/property&gt; &lt;property name="hibernate.c3p0.idle_test_period"&gt;10&lt;/property&gt; &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt; &lt;property name="hibernate.c3p0.max_size"&gt;75&lt;/property&gt; &lt;property name="hibernate.c3p0.max_statements"&gt;10&lt;/property&gt; &lt;property name="hibernate.c3p0.timeout"&gt;50&lt;/property&gt; &lt;property name="hibernate.c3p0.preferredTestQuery"&gt;select 1&lt;/property&gt; &lt;property name="hibernate.c3p0.testConnectionOnCheckout"&gt;true&lt;/property&gt; &lt;!-- Mapping files --&gt; &lt;mapping class="xxx.xxx.xxx.xxx" /&gt; &lt;mapping class="xxx.xxx.xxx.xxx" /&gt; &lt;mapping class="xxx.xxx.xxx.xxx" /&gt; &lt;mapping class="xxx.xxx.xxx.xxx" /&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p><strong>PersistenceManager.java</strong></p> <pre><code>import java.io.PrintStream; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; public class PersistenceManager { private static SessionFactory sessionFactory = null; private static PersistenceManager singleton = null; public static PersistenceManager getInstance() { if (singleton == null) { singleton = new PersistenceManager(); } return singleton; } public SessionFactory getSessionFactory() { if (sessionFactory == null) createSessionFactory(); return sessionFactory; } protected void createSessionFactory() { sessionFactory = new AnnotationConfiguration().configure() .buildSessionFactory(); } public void destroySessionFactory() { if (sessionFactory != null) { sessionFactory.close(); sessionFactory = null; } } } </code></pre> <p><strong>Example 1:</strong></p> <pre><code>import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; public Users Login( String username, String password) { Session session = null; try { String hql = "select u from Users u where u.username like :p1 and u.password like :p2"; session = PersistenceManager.getInstance().getSessionFactory().openSession(); Query q = session.createQuery(hql) .setParameter("p1", username) .setParameter("p2", password); if (q.list().size() == 0) { session.close(); return new Users(); } Users user = (Users)q.list().get(0); session.close(); return user; } catch (Exception e) { session.close(); } } </code></pre> <p><strong>Example 2:</strong></p> <pre><code>import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; public String Registration(Users u) { Session session = null; try { String hql = "select u from Users u where u.username like :p1"; session = PersistenceManager.getInstance().getSessionFactory().openSession(); Query q = session.createQuery(hql).setParameter("p1", u.getUsername()); if (q.list().size() == 0) { session.beginTransaction(); session.persist(u); session.getTransaction().commit(); session.close(); return new Boolean(true).toString(); } session.close(); return new Boolean(false).toString(); } catch (Exception e) { return e.toString(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload