You can always get a PlatformTransactionManager as defined in any example using transactions, for instance:
Code:
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
In java, you can get access to such manager (injection or otherwise) and use it to start/end transaction. Of course, a lot will depend on the transaction definitions of the specific methods you will be calling (i.e. if a method specifies "PROPAGATION_REQUIRES_NEW", this will not help).
Code:
PlatformTransactionManager txMan = ....;
...
...
TransactionDefinition txDef = new DefaultTransactionDefinition(...);
TransactionStatus tx = txMan.getTransaction(txDef);
try {
...
// invoke transactional methods
...
txMan.commit(txDef);
} catch (...) {
txMan.rollback(txDef);
}
This should work, but there're a lot of nuances here, such as proper exception handling, etc.
Another way is to use TransactionTemplate and TransactionCallback from transaction.support package.
Code:
<bean id="txTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
</bean>
public void setTxTemplate(TransactionTemplate txTemplate) {
this.txTemplate = txTemplate;
}
...
...
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
// invoke transactional methods
}
});