wyh5253
wyh5253
采纳率0%
2015-09-15 06:10 阅读 2.3k

Spring整合hibernate4时sessionFactory问题

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.QueryTimeoutException: Could not open connection
org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:440)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy12.findAllUsers(Unknown Source)
org.wjw.action.user.ListUserAction.execute(ListUserAction.java:29)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

7条回答 默认 最新

  • forest_yu forest_yu 2015-09-15 06:19

    初步看有两种可能:1.是你的数据库没有运行起来;2.是你的jndiName没有配置对

    点赞 评论 复制链接分享
  • hekewangzi hekewangzi 2015-09-15 06:28

    就去看看你的SessionFactory有没有配错,,

    点赞 评论 复制链接分享
  • wyh5253 wyh5253 2015-09-15 06:36

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
    <!-- 开启注解功能 -->

    <!-- 引入属性文件 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:*.properties</value>
        </property>
    </bean>
    
    <!--    使用c3p0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name=""></property>
        <!-- 指定连接数据库的驱动-->
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <!-- 指定连接数据库的URL-->
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <!-- 指定连接数据库的用户名-->
        <property name="user" value="${jdbc.user}"/>
        <!-- 指定连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
        <!-- 指定连接池中保留的最大连接数. Default:15-->
        <property name="maxPoolSize" value="35"/>
        <!-- 指定连接池中保留的最小连接数-->
        <property name="minPoolSize" value="10"/>
        <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
        <property name="initialPoolSize" value="5"/>
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
        <property name="maxIdleTime" value="60"/>
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
        <property name="acquireIncrement" value="5"/>
        <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->
        <property name="maxStatements" value="0"/>
        <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
        <property name="idleConnectionTestPeriod" value="60"/>
        <!-- 定义在从数据库获取新连接失败后重复尝试的次数。 Default:30 -->
        <property name="acquireRetryAttempts" value="30"/>
        <!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。Default:false -->
        <property name="breakAfterAcquireFailure" value="true"/>
        <!-- 银性能消耗大请只在需要的时候是哟个它。如果设为true,那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等提升连接测试的性能。 Default:false-->
        <property name="testConnectionOnCheckin" value="true"/>
        <property name="testConnectionOnCheckout" value="true"/>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingLocations">             
            <list>                 
                <value>classpath:mapping/User_hbm.xml</value>             
            </list>         
        </property> 
        <property name="hibernateProperties">   
             <props>   
                 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop><!-- Oracle -->  
                 <prop key="hibernate.show_sql">true</prop> 
                 <prop key="hibernate.autoReconnect">true</prop> 
             </props>   
         </property> 
    </bean>
    
    <!-- 定义事务管理器(声明式的事务) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <!-- 为事务拦截器注入一个事务管理器 -->
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 需要引入tx的命名空间 -->  
    <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager    --> 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes> 
         <!-- 指定哪些方法需要加入事务  -->
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="query*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/> 
            <tx:method name="*" propagation="REQUIRED" />  
        </tx:attributes>  
    </tx:advice>   
    <!-- 需要引入aop的命名空间-->
    <aop:config>     
        <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 --> 
        <aop:pointcut id="daoMethods" expression="execution(* org.wjw.service.impl.*.*(..))" />
        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />  
    </aop:config> 
    
    <bean id="userDAO" class="org.wjw.dao.impl.UserDAOImpl" scope="singleton">   
        <property name="sessionFactory" ref="sessionFactory"/>   
    </bean>  
    
    <bean id="userService" class="org.wjw.service.impl.UserServiceImpl" scope="singleton">   
        <property name="userDao" ref="userDAO" />   
    </bean>   
    
     <bean id="listUserAction" class="org.wjw.action.user.ListUserAction" scope="prototype">    
         <property name="userService" ref="userService"></property>    
     </bean>
    

    点赞 评论 复制链接分享
  • wyh5253 wyh5253 2015-09-15 07:06

    hibernate.cfg.xml
    ?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

    <session-factory>
        <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="connection.url">jdbc:derby://localhost:1527/myeclipse</property>
        <property name="connection.username">classiccars</property>
        <property name="connection.password">myeclipse</property>
        <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
        <property name="myeclipse.connection.profile">MyEclipse Derby</property>
    </session-factory>
    

    dao层
    package org.wjw.dao.impl;

    import java.util.List;

    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.wjw.dao.UserDAO;
    import org.wjw.entity.User;

    public class UserDAOImpl implements UserDAO {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public void setSessionFactory(SessionFactory sessionFasFactory) {
        this.sessionFactory = sessionFasFactory;
    }
    
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
    
    
    public void doCreateUser(User user) { 
        Session session = getSession();
        session.save(user);  
    }  
    
    @SuppressWarnings("unchecked")  
    public List<User> findAllUsers() {
        System.out.println("开始查询用户列表");
        Session session = getSession();
        String hql = "from User u order by u.id asc";  
        List<User> list = session.createQuery(hql).list();  
        return list;  
    }  
    
    public void removeUser(User user) { 
        Session session = getSession();
        session.delete(user);  
    }  
    
    public void updateUser(User user) {  
        Session session = getSession();
        session.update(user);  
    }  
    
    public User findUserById(int id) {  
        Session session = getSession();
        User user = null;  
        user = (User) session.get(User.class, id);  
        return user;  
    }  
    
    @SuppressWarnings("unchecked")
    @Override
    public List<User> queryForPage(final String hql, final int offset, final int length) {    
        Session session = this.getSession();    
        Query query = session.createQuery(hql);    
        query.setFirstResult(offset);    
        query.setMaxResults(length);
        List<User> list = query.list();    
        return list;    
    }    
    
    @Override
    public int getTotalCount(String hql) {
        Session session = getSession();
        return session.createQuery(hql).hashCode(); 
    
    }
    

    }

    点赞 评论 复制链接分享
  • hekewangzi hekewangzi 2015-09-15 07:18

    你用的是哪个数据库啊,,你确定能连上??

    点赞 评论 复制链接分享
  • wyh5253 wyh5253 2015-09-15 07:24

    如果数据库配置错误 Tomcat就启动不了啊 我的是在进行操作时抛异常的

    点赞 评论 复制链接分享
  • wyh5253 wyh5253 2015-09-15 08:34

    问题解决了 是我的疏忽 数据库参数配置重了 hibernate.cfg.xml里也配置了参数 然而几个参数是工具生成的 去掉就可以了

    点赞 评论 复制链接分享

相关推荐