0

I have a context by name dao-context.xml as following:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="true" destroy-method="close">
<!--   define data source properties  -->
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     <!--   define another properties  -->
</beans>

<!-- define dao object and set jpaTemplate -->

<bean id="doa_obj" class="DaoObject">
    <!-- set jpa template -->
</bean>

and load this context as following:

public class DaoFactory
{
    private static ApplicationContext dao_ctx = new ClassPathXmlApplicationContext("dao-context.xml");
    public static ApplicationContext getDaoCtx()
    {
        return dao_ctx;
    }
}

then I define Biz Layer context as following with name "biz_layer.xml":

<bean id="ctx" class="dao.DaoFactory" factory-method="getDaoCtx"/>

<bean factory-bean="ctx" factory-method="getBean" id="emf">
    <constructor-arg index="0" value="entityManagerFactory"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>

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

<bean id="biz_obj" class="BizObject" />

and BizObject define as following:

@Transactional
public class BizObject
{
     @Transactional
     public void issueDoc()
     {
         ((DaoObject)DaoFactory.getDaoCtx().getBean("Dao_Obj")).save(new DaoEntity('Arture'));
     }
}

and in end I have a main method as following:

Public class Main
{
    public static void main(String[] args)
    {
         ApplicationContext biz_ctx = new ClassPathXmlApplicationContext("biz_layer.xml");
         BizObject bizObj = (BizObject)biz_ctx.getBean("biz_obj");
         bizObj.issueDoc();
    }
}      

I with test by several scenarios.

  • When I run Main class any exception not raise but any record not committed on data base.
  • When I debug Main class, bizObj variable is not a proxy.
  • When I define biz_obj bean to dao_context.xml and in Main class initialize it , bizObj is proxy and @Transactional worked fine and committed into data base.
  • When I programmatically manage Transaction all thing worked fine.

My question is:

What when define all of bean contain Dao layer bean and Biz layer beans in same context all thing worked fine but when separate then in separate context @Transactional annotation not worked and any exception not raise so.

Sam
  • 6,770
  • 7
  • 50
  • 91
  • It will not work this way also you shouldn't load the context yourself. You should have a single context loading all xml files. Also imho this explicit layering is a bad thing, especially with large applications. – M. Deinum Dec 18 '14 at 14:00
  • please explain your reason for this layering bad idea. – Sam Dec 18 '14 at 15:29
  • You shouldn't do an explicit technical layering, things that belong together should be put together. 25 services in a service package don't belong together. Now you make it even worse by making each layer an explicit application context, especially when using AOP (transactions) this can be painful. With transactions you could have 2 different instances of your transaction mechanism interfering with each other. – M. Deinum Dec 18 '14 at 18:04

0 Answers0