title: Hibernate SQL Error: 17002, SQLState: null Io exception: Connection reset
body: <p>We're using hibernate 3 and c3p0-0.9.1. We've an Hibernate criteria based query to check the keyed in user name and password from database tables as well as other queries. During execution of first query i.e the login page, we're facing the connecion reset issue with SQLState as null. If provide the details again in the page, everything is working on fine for some time. If we try after some time, again the same problem for the first time and when re-enter the user name and password, it is working fine for sometime. Please find the below error messages for reference. </p>
  
  <p><strong>00:12:42 WARN  [org.hibernate.util.JDBCExceptionReporter:77 logExceptions] - SQL Error: 17002, SQLState: null
  00:12:42 ERROR [org.hibernate.util.JDBCExceptionReporter:78 logExceptions] - Io exception: Connection reset</strong></p>
  
  <p>Here is the sample code...</p>
  
  <pre><code>public UserSample fetchUserDetails(final String loginId) throws DataAccessException {    
         return (UserSample) getHibernateTemplate().execute(new HibernateCallback() {
              public Object doInHibernate(Session session)
                      throws HibernateException, SQLException {
  
                  Criteria criteria = getSession().createCriteria(UserSample.class)
                      .createAlias("rolesample", "rolesample")
                      .createAlias("accountsample", "accountsample");
                  try {
                      if(loginId!= null){
                          criteria.add(Restrictions.eq("loginName",loginId));
                      }
  
              criteria.setProjection(Projections.projectionList()
                  .add(Projections.property("loginName").as("loginName2"))
                  .add(Projections.property("password").as("password"))
                  .add(Projections.property("name").as("name"))
                  .add(Projections.property("roleId").as("roleId"))               );
  
                  criteria.addOrder(Order.desc("loginName"));
                  criteria.setFetchSize(1);
                  criteria.setMaxResults(1);
                  criteria.setResultTransformer(Transformers.aliasToBean(UserSample.class));
  
                  ArrayList list = (ArrayList)criteria.list();
                  if(list != null &amp;&amp; list.size() &gt; 0) {
                      return  (User)criteria.list().get(0);
                  }else{
                      return new User();
                  }
              }
                  catch (Exception e) {
                      LogUtil.info(e.getMessage());
                      return null;
                  }
              }
          }); 
     }
  </code></pre>
  
  <p>The connection pooling in applicationContext.xml is as below:</p>
  
  <pre><code>&lt;bean id="sampleSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="sampleDataSource" /&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.OracleDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.query.substitutions"&gt;true 1, false 0, yes 'Y', no 'N'&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.pool_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt;                
                  &lt;prop key="hibernate.connection.provider_class"&gt;org.hibernate.connection.C3P0ConnectionProvider&lt;/prop&gt;                
  
                  &lt;!-- C3P0 Properties --&gt;
                  &lt;prop key="hibernate.c3p0.min_size"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.timeout"&gt;1800&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.max_statements"&gt;100&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.idle_test_period"&gt;600&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.acquire_increment"&gt;1&lt;/prop&gt;
                  &lt;prop key="hibernate.c3p0.preferredTestQuery"&gt;SELECT 1 FROM DUAL&lt;/prop&gt;
              &lt;/props&gt;            
          &lt;/property&gt;
  </code></pre>
  
  <p>When I executed the generated query in oracle client such as SQL deverloper, able to get the result. But, the when run through the application, facing this issue randomly. Can you please assist? Thanks.</p>
  
  <p>Best Regards,
  Lucky </p>