java.lang.IllegalStateException: SessionFactory not initialized yet

Spring JIRA | Adrian Moos | 2 years ago
  1. 0

    When a @Bean method returns an instance of a FactoryBean, Spring proxies the factory bean, redirecting calls to getObject() to applicationContext.getBean(). The relevant code reads: {code} /** * Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory * instead of creating a new instance. These proxies are created only when calling a FactoryBean from * within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean * instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, * it will not be proxied. This too is aligned with the way XML configuration works. */ private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory, final String beanName) throws InstantiationException, IllegalAccessException { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(fbClass); enhancer.setUseFactory(false); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("getObject") && args.length == 0) { return beanFactory.getBean(beanName); } return proxy.invokeSuper(obj, args); // bug here? } }); return enhancer.create(); } {code} In the marked line, obj refers to the proxy object. Therefore, calls to methods other than getObject() are forwarded to the super implementation *on the proxy object*, which has a different state than the FactoryBean it proxies. This breaks the following usecase: {code} @Bean protected DBTool dbTool() { return new DBTool(hibernateSessionFactory()); } @Bean protected AnnotationSessionFactoryBean hibernateSessionFactory() { // hibernate setup goes here } {code} where DBTool has a method: {code} public void createSchema() { annotationSessionFactory.createDatabaseSchema(); } {code} which now throws {code} java.lang.IllegalStateException: SessionFactory not initialized yet at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>) at ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23) {code} because the proxy object is a factory that has not been configured.

    Spring JIRA | 2 years ago | Adrian Moos
    java.lang.IllegalStateException: SessionFactory not initialized yet
  2. 0

    When a @Bean method returns an instance of a FactoryBean, Spring proxies the factory bean, redirecting calls to getObject() to applicationContext.getBean(). The relevant code reads: {code} /** * Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory * instead of creating a new instance. These proxies are created only when calling a FactoryBean from * within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean * instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, * it will not be proxied. This too is aligned with the way XML configuration works. */ private Object enhanceFactoryBean(Class<?> fbClass, final ConfigurableBeanFactory beanFactory, final String beanName) throws InstantiationException, IllegalAccessException { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(fbClass); enhancer.setUseFactory(false); enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE); enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (method.getName().equals("getObject") && args.length == 0) { return beanFactory.getBean(beanName); } return proxy.invokeSuper(obj, args); // bug here? } }); return enhancer.create(); } {code} In the marked line, obj refers to the proxy object. Therefore, calls to methods other than getObject() are forwarded to the super implementation *on the proxy object*, which has a different state than the FactoryBean it proxies. This breaks the following usecase: {code} @Bean protected DBTool dbTool() { return new DBTool(hibernateSessionFactory()); } @Bean protected AnnotationSessionFactoryBean hibernateSessionFactory() { // hibernate setup goes here } {code} where DBTool has a method: {code} public void createSchema() { annotationSessionFactory.createDatabaseSchema(); } {code} which now throws {code} java.lang.IllegalStateException: SessionFactory not initialized yet at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215) at org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383) at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>) at ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23) {code} because the proxy object is a factory that has not been configured.

    Spring JIRA | 2 years ago | Adrian Moos
    java.lang.IllegalStateException: SessionFactory not initialized yet
  3. 0

    Terracotta 3.5.1 On JBoss 6

    Terracotta | 6 years ago | urciolo
    java.lang.IllegalStateException: not yet initialized
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.lang.IllegalStateException

      SessionFactory not initialized yet

      at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory()
    2. Hibernate
      AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke
      1. org.springframework.orm.hibernate3.AbstractSessionFactoryBean.getSessionFactory(AbstractSessionFactoryBean.java:215)
      2. org.springframework.orm.hibernate3.LocalSessionFactoryBean.createDatabaseSchema(LocalSessionFactoryBean.java:989)
      3. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.CGLIB$createDatabaseSchema$12(<generated>)
      4. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc$$FastClassBySpringCGLIB$$52787a0.invoke(<generated>)
      4 frames
    3. Spring Core
      MethodProxy.invokeSuper
      1. org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      1 frame
    4. Spring Context
      ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept
      1. org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor$1.intercept(ConfigurationClassEnhancer.java:383)
      1 frame
    5. Hibernate
      AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema
      1. org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean$$EnhancerBySpringCGLIB$$2fba14bc.createDatabaseSchema(<generated>)
      1 frame
    6. ch.bedag.ste
      DBTool.createSchema
      1. ch.bedag.ste.app.cf.hibernate.framework.DBTool.createSchema(DBTool.java:23)
      1 frame