Tag Archives: transaction

Spring with multiple transaction managers

zt4PX
Here is repost of http://stackoverflow.com/questions/4423125/spring-is-it-possible-to-use-multiple-transaction-managers-in-the-same-applica question.

Here is applicationContext.xml config part:

<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager"> 
  <property name="entityManagerFactory" ref="entityManagerFactory1" /> 
  <qualifier value="account"/> 
</bean>

<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory2" /> 
  <qualifier value="businessData"/> 
</bean>

and code using:

public class TransactionalService {

    @Transactional("account")
    public void setSomethingInAccount() { ... }

    @Transactional("businessData")
    public void doSomethingInBusinessData() { ... }
}

So, as you see we can simply use several connections with transaction management in Spring application.

See also,
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-multiple-tx-mgrs-with-attransactional
Enjoy!

Sql queries under transaction in Groovy

trans
When you wish to perform some sql queries in transaction, use withTransaction method:

this.class.classLoader.rootLoader.addURL(
  new URL("file:///$jbossPath/lib/ojdbc6.jar"))
Sql sql = Sql.newInstance("ds.url", "ds.user.name", 
  "ds.user.password", "oracle.jdbc.driver.OracleDriver")

sql.withTransaction {
  sql.eachRow("SELECT * FROM USERS") {
    sql.executeUpdate("update USERS set PASSWORD = ? where USER_ID = ?", 
      ["123456", it.USER_ID])
  }
}