spring - Choose between muliple transaction managers at runtime -
i have 2 clients using same spring based rest application deployed in tomcat. depending on client need choose between data sources , transaction manager. how choose @ runtime, transaction manager use?
<bean id="first_datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="url" value="${first_jdbc.url}" /> <property name="driverclassname" value="${first_jdbc.driverclassname}" /> <property name="username" value="${first_jdbc.username}" /> <property name="password" value="${first_jdbc.password}" /> <property name="removeabandoned" value="true" /> <property name="initialsize" value="20" /> <property name="maxactive" value="30" /> <!-- <property name="defaultautocommit" value="false" /> --> </bean> <bean id="second_datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="url" value="${second_jdbc.url}" /> <property name="driverclassname" value="${second_jdbc.driverclassname}" /> <property name="username" value="${second_jdbc.username}" /> <property name="password" value="${second_jdbc.password}" /> <property name="removeabandoned" value="true" /> <property name="initialsize" value="20" /> <property name="maxactive" value="30" /> <!-- <property name="defaultautocommit" value="false" /> --> </bean> <bean id="first_transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager" scope="singleton"> <property name="datasource" ref="first_datasource" /> <qualifier value="sc_tm"></qualifier> </bean> <bean id="second_transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager" scope="singleton"> <property name="datasource" ref="second_datasource" /> <qualifier value="second_tm"></qualifier> </bean>
in code how choose @transactional("????") @ run time. if not possible org.springframework.jdbc.datasource.datasourcetransactionmanager there other way of doing it?
using @transactional
, can specify transaction manager this:
@transactional("first_transactionmanager")
or
@transactional("second_transactionmanager")
depending on 1 want use. make sure use inside transactional method correct entity manager / session factory. have specify 1 want inject @persistencecontext("nameofpersistenceunit")
.
Comments
Post a Comment