Results 1 to 4 of 4

Thread: ERROR again... No Hibernate Session bound to thread...

  1. #1

    Default ERROR again... No Hibernate Session bound to thread...

    Hi i got this Exception:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:687)
    de.fzi.hiwitool.db.tables.contract.dao.ContractDAO Impl.save(ContractDAOImpl.java:51)
    de.fzi.hiwitool.db.tables.contract.service.Contrac tServiceImpl.addContract(ContractServiceImpl.java: 24)
    de.fzi.hiwitool.controllers.HomeController.home(Ho meController.java:49)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    ...
    I found no information in this forum or WWW to solve the Problem.. pls help me

    My configuration:

    root-context.xml
    Code:
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">
    
    	<!-- SEE DatabaseConfiguration.java -->
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<context:component-scan base-package="com.mycomp.test" />
    </beans>
    servlet-context.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    	
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	
    	<context:component-scan base-package="com.mycomp.test" />
    	
    	<context:annotation-config />
    	
    	
    </beans:beans>
    DatabaseConfiguration.java
    Code:
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.hibernate3.HibernateTransactionManager;
    import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
    
    import com.mysql.jdbc.Driver;
    
    @Configuration
    public class DatabaseConfiguration {
    
    	@Bean
    	public DataSource myDataSource() {
    
    		DriverManagerDataSource dataSource = new DriverManagerDataSource();
    		dataSource.setDriverClassName(Driver.class.getName());
    		dataSource.setUrl("jdbc:mysql://localhost/test_db");
    		dataSource.setUsername("root");
    		dataSource.setPassword("");
    
    		return dataSource;
    	}
    
    	@Bean
    	public AnnotationSessionFactoryBean sessionFactoryBean() {
    
    		Properties props = new Properties();
    		props.put("hibernate.dialect", UTF8MysqlDialect.class.getName());
    		props.put("hibernate.format_sql", "true");
    		props.put("hibernate.connection.charSet", "UTF-8");
    		props.put("hibernate.connection.useUnicode", "true");
    		props.put("hibernate.connection.characterEncoding", "UTF-8");
    
    		AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
    		bean.setPackagesToScan(new String[]{"com.mycomp.test.db.tables"});
    		bean.setHibernateProperties(props);
    		bean.setDataSource(myDataSource());
    		bean.setSchemaUpdate(true);
    
    		return bean;
    	}
    
    	@Bean
    	public HibernateTransactionManager transactionManager() {
    		return new HibernateTransactionManager(sessionFactoryBean().getObject());
    	}
    }
    DAO/Service Design

    Code:
    @Transactional
    public interface IContractService {
    
    	void addContract(Contract contract) throws DBException;
    
    	List<Contract> listContracts(Object hiwiID) throws DBException;
    
    	void deleteByID(Long contractID) throws DBException;
    }
    Code:
    @Service
    public class ContractServiceImpl implements IContractService {
    
    	@Autowired
    	private IContractDAO _contractDAO;
    
    	public void addContract(Contract contract) throws DBException {
    		_contractDAO.save(contract);
    	}
    
    	public List<Contract> listContracts(Object hiwiID) throws DBException {
    		return _contractDAO.getByHiwiID(hiwiID);
    	}
    
    	public void deleteByID(Long contractID) throws DBException {
    		_contractDAO.deleteByID(contractID);
    	}
    
    }
    DAO
    Code:
    public interface IContractDAO {
    
    	void save(Contract contract) throws DBException;
    
    	List<Contract> getByHiwiID(Object id) throws DBException;
    
    	void update(Contract contract) throws DBException;
    
    	void deleteByID(Serializable contractID) throws DBException;
    }
    Code:
    @Repository
    public class ContractDAOImpl implements IContractDAO{
    
        @Autowired
        private SessionFactory sessionFactory;
    	
    	public ContractDAOImpl() {
    	}
    	
    	public void delete(Contract contract) throws DBException {
    		Session s = sessionFactory.getCurrentSession();
    		s.delete(contract);
    	}
    
    	public void update(Contract contract) throws DBException {
    		Session s = sessionFactory.getCurrentSession();
    		s.update(contract);
    	}
    	public void save(Contract contract) throws DBException {
    		Session s = sessionFactory.getCurrentSession();
    		s.save(contract);
    	}
    
    	@SuppressWarnings("unchecked")
    	public List<Contract> getByHiwiID(Object id) throws DBException {
    		[...]			
    	}
    }
    Spring!! New Version -> New Tutorial

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default


    I found no information in this forum or WWW to solve the Problem.. pls help me
    I strongly suggest then that you improve your search skills as this question has been answered numerous times before (by me alone already).

    IN short you are scanning for the same component twice leading to duplicate instances 1 proxies one not proxied and as the not proxied one is in the same context as your controller that one is used.

    Another note your hibernate.connection properties are useless as you are injecting a datasource so those properties are ignored.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Quote Originally Posted by Marten Deinum View Post
    I strongly suggest then that you improve your search skills as this question has been answered numerous times before (by me alone already).

    IN short you are scanning for the same component twice leading to duplicate instances 1 proxies one not proxied and as the not proxied one is in the same context as your controller that one is used.

    Another note your hibernate.connection properties are useless as you are injecting a datasource so those properties are ignored.
    I just had add @EnableTransactionManagement to my DatabaseConfiguration...

    problem solved.
    Spring!! New Version -> New Tutorial

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Although it solves your problem you still have a problem that all your beans are duplicated and that is your fundamental problem what you have done now is also duplicate transaction management and proxy creation. So I strongly would suggest to fix the real problem instead of increasing your memory usage. But that is IMHO...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •