Results 1 to 3 of 3

Thread: No Hibernate Session Bound Error

  1. #1

    Default No Hibernate Session Bound Error

    Hello,

    I am getting the exception. Any suggestions as to what may be wrong would be greatly appreciated.

    thanks

    Exception:

    Code:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    	at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:299)
    	at $Proxy0.getCurrentSession(Unknown Source)
    	at org.openiam.base.BaseHibernateDAO.add(BaseHibernateDAO.java:55)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
    	at $Proxy2.add(Unknown Source)
    	at org.openiam.idm.srvc.continfo.EmailAddressDAOTest.testAddAddress(EmailAddressDAOTest.java:57)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at junit.framework.TestCase.runTest(TestCase.java:164)
    EmailAddressDAOTest:

    Code:
    public class EmailAddressDAOTest extends AbstractDependencyInjectionSpringContextTests  {
    
    	ApplicationContext ctx = null;
    	EmailAddress eml1,eml2,eml3;
    	EmailAddressDAO adrDAO;
    	
    	@Override
    
    	protected void onSetUp() throws Exception {
    		// TODO Auto-generated method stub
    		super.onSetUp();
    		ctx = new ClassPathXmlApplicationContext(
    					new String[] {"/applicationContext.xml",
    								  "/userTest-applicationContext.xml"} ) ;
    
    		 eml1= (EmailAddress)ctx.getBean("email1");
    		 eml2 = (EmailAddress)ctx.getBean("email2");
    		 eml3 = (EmailAddress)ctx.getBean("email3");
    		adrDAO = (EmailAddressDAO)ctx.getBean("emailAddressDAO");
    		
    		
    	} 
    
    	
    	/* Test direct address methods */
    	
    	@Test
    	public void testAddAddress() {
    				
    		adrDAO.add(eml1); 		
    		
    		EmailAddress tempAdr = adrDAO.findById(eml1.getEmailAddress());
    		assertEquals(eml1.getEmailAddress(), tempAdr.getEmailAddress());
    		
    	}
    BaseHibernateDAO Class:

    Code:
    public abstract class BaseHibernateDAO <T, ID extends Serializable> implements BaseDAO<T, ID> {
    
    	protected Class<T> persistentClass;
    	protected SessionFactory sessionFactory;
    	protected static Log log = null;
    
        public BaseHibernateDAO() {
            this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                                    .getGenericSuperclass()).getActualTypeArguments()[0];
         }
        
    
    	public void setSessionFactory(SessionFactory session) {
    		   this.sessionFactory = session;
    		   this.getClass();
    	}
    
    	protected SessionFactory getSessionFactory() {
    		try {
    			return (SessionFactory) new InitialContext().lookup("SessionFactory");
    		} catch (Exception e) {
    			log.error("Could not locate SessionFactory in JNDI", e);
    			throw new IllegalStateException(
    					"Could not locate SessionFactory in JNDI");
    		}
    	}
    	
        public Class<T> getPersistentClass() {
            return persistentClass;
        }
    	
    
    
    	/**
    	 * Adds a new instance
    	 * @param instance
    	 */
    	public void add(T instance) {
    		log.debug("persisting instance");
    		try {
    			sessionFactory.getCurrentSession().persist(instance);
    			log.debug("persist successful");
    		} catch (RuntimeException re) {
    			log.error("persist failed", re);
    			throw re;
    		}
    
    	}
    EmailAddressDAOImpl

    Code:
    public class EmailAddressDAOImpl extends BaseHibernateDAO<EmailAddress, String> implements EmailAddressDAO {
    
    
    	{
    		log = LogFactory.getLog(EmailAddressDAOImpl.class);
    	}
    ApplicationContext.xml

    Code:
    	<bean id="emailAddressDAO" class="org.openiam.idm.srvc.continfo.service.EmailAddressDAOImpl" >
    		<property name="sessionFactory" ref="sessionFactory"></property>		
    	</bean>	
    
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="mappingResources">
    			<list>
    				.....			
                            </list>
    		</property>
                 .....
            </bean>
    
    
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
      	</bean>
    
    
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
          <!-- all methods starting with 'get' are read-only -->
          <tx:method name="get*" read-only="true" propagation="SUPPORTS" />
          <!-- other methods use the default transaction settings (see below) -->
          <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
      </tx:advice>
    
      <aop:config>
        <aop:pointcut id="emailAddressDAOOperation" expression="execution(* org.openiam.idm.srvc.continfo.service.EmailAddressDAOImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="emailAddressDAOOperation"/>
      </aop:config>

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Your transactional advice isn't matching. I think you need to reference the interface instead of the class in the expression e.g. EmailAddressDAO. I would also have a look at the transactional spring unit tests they might make things easier. You are also creating an applicationContext in the test which you don't need to do.
    Last edited by karldmoore; Aug 27th, 2007 at 02:32 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

    You need to extend AbstractTransactionalSpringContextTests

    and add the following method to your test case
    public void setTransactionManager(PlatformTransactionManager tm){
    super.setTransactionManager(tm);
    }

Posting Permissions

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