title: Cause: java.sql.SQLException: connection is closed
body: <ol>
  <li>db : Mydql</li>
  <li>connection pool ： Druid</li>
  <li>jdk7</li>
  <li><p>mybatis</p>
  
  <pre><code>org.springframework.jdbc.UncategorizedSQLException:
  ### Error querying database.  Cause: java.sql.SQLException: connection is closed
  ### The error may exist in class path resource [mybatis1/hotel/StopRoomRecordMapper.xml]
  ### The error may involve StopRoomRecordMapper.getByModel
  ### The error occurred while executing a query
  ### Cause: java.sql.SQLException: connection is closed
  ; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; connection is closed; nested exception is java.sql.SQLException: connection is closed
          at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:90)
          at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
          at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
          at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
          at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
          at com.sun.proxy.$Proxy16.selectOne(Unknown Source)
          at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
          at com.leyizhu.dao.DaoSupport.findForObject(DaoSupport.java:155)
          at com.leyizhu.dao.hotel.impl.StopRoomRecordDaoImpl.getByRoomTypeIdAndTradeDate(StopRoomRecordDaoImpl.java:96)
          at com.leyizhu.dao.hotel.impl.StopRoomRecordDaoImpl.save(StopRoomRecordDaoImpl.java:34)
          at com.leyizhu.service.hotel.impl.StopRoomRecordService.save(StopRoomRecordService.java:31)
          at com.leyizhu.service.hotel.impl.StopRoomRecordService$$FastClassBySpringCGLIB$$5ef83be9.invoke(&lt;generated&gt;)
          at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
          at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
          at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
          at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
          at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
          at com.leyizhu.service.hotel.impl.StopRoomRecordService$$EnhancerBySpringCGLIB$$b431adda.save(&lt;generated&gt;)
          at com.leyizhu.task.timer.client.RecordStopRoom.execute(RecordStopRoom.java:54)
          at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
          at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
  Caused by: java.sql.SQLException: connection is closed
          at com.alibaba.druid.pool.DruidPooledConnection.checkState(DruidPooledConnection.java:1050)
          at com.alibaba.druid.pool.DruidPooledConnection.getAutoCommit(DruidPooledConnection.java:722)
          at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81)
          at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:66)
          at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:315)
          at org.apache.ibatis.executor.ReuseExecutor.prepareStatement(ReuseExecutor.java:78)
          at org.apache.ibatis.executor.ReuseExecutor.doQuery(ReuseExecutor.java:58)
  </code></pre></li>
  </ol>
  
  <p><strong>mycode</strong>:</p>
  
  <pre><code>@Override
  public void save(StopRoomRecord model) throws Exception {
      StopRoomRecord stopRoomRecord = getByRoomTypeIdAndTradeDate(model.getRoomtypeid(), model.getTradedate());
      if (stopRoomRecord != null) {
          //存在则更新数据
          model.setId(stopRoomRecord.getId());
          update(model);
      } else {
          //否则新增
          model.setId(UuidUtil.get32UUID());
          model.setCreatetime(new Date());
          dao.save("StopRoomRecordMapper.save", model);
      }
  }
  </code></pre>