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 ondata base
. - When I debug
Main
class,bizObj
variable is not a proxy. - When I define
biz_obj
bean todao_context.xml
and inMain
class initialize it ,bizObj
is proxy and@Transactional
worked fine and committed intodata 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.