0

I'm getting exception: No Session found for current thread, when I want to connect with database via hibernate, my cfg files: 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>

dispatcher 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>

I tried one solution (adding @Transactional annotation in service layer on method connecting to hibernate,but now i'm getting 2 exceptions: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection, any solutions? -maybe the problem is related with this: Cannot load JDBC driver class 'com.postgresql.jdbc.Driver' -no one was able to help me, I have all neccesary dependencies, but don't know what's wrong, thanks

Community
  • 1
  • 1
Charlie Harper
  • 379
  • 4
  • 7
  • 21

3 Answers3

0

You cannot connect because the driver is not on the classpath. You can download from here: http://jdbc.postgresql.org/download.html.

It could solve your jdbc class loading problems.

Lexandro
  • 765
  • 5
  • 14
  • I don't exactly understand what should i do, download the jar and could You please explain me how to add it into classpath? but i forgot to write, I'm using maven, i have right dependency, and if you look at this picture- http://screenshot.cz/6YJM4/, you can see i have the jar in my external libraries(and in the .jar is the required Driver class) – Charlie Harper Jun 28 '14 at 08:42
  • Hm. Is it added to your pom.xml? In this case it should be on your classpath. What is the stacktrace? – Lexandro Jun 28 '14 at 08:47
  • yes i have right dependency, I'm sure, stack trace for first case:http://screenshot.cz/UT91N/, and for second:http://screenshot.cz/AHH0Z/, it's probably for that warning, because Driver is not loaded, im desperated :/ – Charlie Harper Jun 28 '14 at 08:53
  • 2
    @CharlieHarper The classname of the JDBC driver is normally `org.postgresql.Driver` and not `com.postgresql.jdbc.Driver`. Verify it by looking into the jar with any zip tool. – blafasel Jun 28 '14 at 13:17
  • I would like to see the startup log stacktrace, not the request's please – Lexandro Jun 28 '14 at 13:30
  • solved: i typed bad connection url: 8080 instead of 5432, uf and driver class was bad too, thanks guys – Charlie Harper Jun 28 '14 at 16:25
0

Make sure you have wired in the SessionFactory bean into your dao implementation class. One way of doing that is as follow:

@Resource
private SessionFactory mySessionFactory;
0

Let's start the following segment:

<property name="driverClassName" value="com.postgresql.jdbc.Driver"/>

I used "com.postgresql.jdbc.Driver" as well for driverClassName and got the same error as you are getting. The only difference is I gave the database connection details in a resource.xml file and used resource injection to initiate the database connectivity from Java files.

<Context>

  <Resource name="jdbc/test" 
            auth="Container" type="javax.sql.DataSource"
               maxActive="20" maxIdle="5" maxWait="10000"
               username="postgres" password="password" 
               driverClassName="org.postgresql.Driver"
               url="jdbc:postgresql://localhost:5432/test?allowPublicKeyRetrieval=true"/>
                       
</Context>

My issue got resolved and there are no issues with database connectivity after I have used driverClassName="org.postgresql.Driver" instead of driverClassName="com.postgresql.jdbc.Driver".

Try the below code instead:

<property name="driverClassName" value="org.postgresql.Driver"/> 

Clean the project once or multiple times before running with the new code. Once the changes get implemented, it starts working properly.

Anish Narayan
  • 72
  • 2
  • 8