会话问题(休眠)

时间:2014-06-28 08:32:20

标签: java spring hibernate postgresql

我遇到异常:当我想通过hibernate,我的cfg文件连接数据库时,找不到当前线程的会话: web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <display-name>Here we blog web application</display-name>

    <servlet>
        <servlet-name>web-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>web-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-dispatcher-servlet.xml</param-value>
    </context-param>

</web-app>

调度员servlet:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.lime"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.postgresql.jdbc.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:8080/come_to_blog_db"/>
        <property name="username" value="postgres"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan">
            <array>
                <value>com.lime</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
            </value>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

我尝试了一种解决方案(在连接到hibernate的方法的服务层中添加@Transactional注释,但现在我得到2个例外: 无法打开Hibernate Session进行事务处理;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接,任何解决方案? - 问题可能与此有关:Cannot load JDBC driver class 'com.postgresql.jdbc.Driver' - 没有人能够帮助我,我有所有必要的依赖,但不知道什么是错的,谢谢

2 个答案:

答案 0 :(得分:0)

您无法连接,因为驱动程序不在类路径中。 您可以从这里下载:http://jdbc.postgresql.org/download.html

它可以解决你的jdbc类加载问题。

答案 1 :(得分:0)

确保已将SessionFactory bean连接到dao实现类中。 这样做的一种方法如下:

@Resource
private SessionFactory mySessionFactory;