连接使用dbcp 2超时

时间:2015-09-22 10:57:22

标签: java mysql spring hibernate struts

我在我的项目中使用struts 1.2,hibernate 3,spring 2和tomcat作为服务器。我已经使用dbcp 2(jdk版本1.7.0_03)实现了连接池。我使用MySql作为数据库。尽管实现了dbcp2,但连接每天都会超时。我收到以下异常。

org.hibernate.transaction.JDBCTransaction - ERROR - JDBC begin failed
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
    at com.mysql.jdbc.ConnectionImpl.getMutex(ConnectionImpl.java:3015)
    at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4789)
    at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:237)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:63)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:496)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:322)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:255)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:102)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:615)
    at com.tcg.service.user.UserService$$EnhancerByCGLIB$$dc6eec0f.getUserDetails(<generated>)
    at com.tcg.delegate.user.UserDelegate.getUserDetails(UserDelegate.java:127)
    at com.tcg.common.CommonUtil.InactiveUser(CommonUtil.java:209)
    at com.tcg.util.AutomatedProcess.run(AutomatedProcess.java:54)
    at java.lang.Thread.run(Thread.java:722)

我已经搜索了解决方案,但建议的解决方案无法解决我的问题。

这是我的hibernate配置文件:

<hibernate-configuration>

    <session-factory name="hibernate.session-factory.SRS">
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
        <property name="hibernate.default_schema">ITSDEV</property>
        <property name="show_sql">false</property>
        <property name="hibernate.jdbc.batch_size">10</property>
        <property name="hibernate.cache.use_second_level_cache">false</property>
        <property name="hibernate.cache.use_query_cache">false</property>

    </session-factory>
</hibernate-configuration>

这是我的应用程序上下文文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <!-- My SQL database Connection -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://172.20.25.185:3306/itsdev"/> 
      <property name="username" value="itsdev"/>
      <property name="password" value="itsdev"/>      
    </bean> 
</beans>

这是我在我的应用程序中使用的示例DAO类:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.tcg.common.CommonUtil;
import com.tcg.dto.ItsUserMaster;

public class CopyOfLoginCheckDAO extends HibernateDaoSupport{
   @SuppressWarnings({ "unchecked"})
    public boolean CheckCredentials(String loginId, String password, Map map)  {
        boolean retVal = false;
        List<ItsUserMaster> userData = new ArrayList<ItsUserMaster>();
        try{

        HibernateTemplate template = getHibernateTemplate(); 
        userData=template.find("from ItsUserMaster itsUserMaster where itsUserMaster.loginId='"+loginId+"' and itsUserMaster.loginCred='Y' and itsUserMaster.deleteFlag!='Y' and itsUserMaster.active!='I' and itsUserMaster.itsCorporateMaster.active!='I' and itsUserMaster.itsCorporateMaster.deleteFlag!='Y' and itsUserMaster.itsUserTypeMaster.deleteFlag!='Y' and itsUserMaster.itsUserTypeMaster.active!='I' and itsUserMaster.itsDepartmentMaster.active!='I' and itsUserMaster.itsDepartmentMaster.deleteFlag!='Y'");

        if(userData.size()>0){

            ItsUserMaster itsUserMaster = (ItsUserMaster)userData.get(0);
            String pwd = CommonUtil.decode(itsUserMaster.getPassword().toString());
            String loginCred=itsUserMaster.getLoginCred().toString();
            if(loginCred.equalsIgnoreCase("Y")){
                retVal = true;
            }

        }
        } catch (Exception sqlexcep) {          
            sqlexcep.printStackTrace();
            retVal = false;
            System.out.println("Exception occured while getting Connection to the database");
        } 
        return retVal;
    }

}

如果您需要更多信息,请告诉我。我感谢您的帮助。感谢。

0 个答案:

没有答案