Feeds:
Posts
Comments

Archive for the ‘spring’ Category

For required jars have a look into my earlier post on jotm or atomikos.

app-context.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans
  xmlns=”http://www.springframework.org/schema/beans
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>
<bean id=”jotm” class=“org.springframework.transaction.jta.JotmFactoryBean”/>
<bean id=”txManager”
 class=”org.springframework.transaction.jta.JtaTransactionManager”>
 <property name=”userTransaction”><ref local=”jotm”/></property>
 <!– <property name=”transactionManagerName” value=”java:comp/UserTransaction” /> –>
</bean>
 

<bean id=”dataSourceDb1″ destroy-method=”shutdown”>
   <property name=”dataSource”>
      <bean destroy-method=”shutdown”>
         <property name=”transactionManager” ref=”jotm” />
         <property name=”driverName” value=”com.mysql.jdbc.Driver” />
         <property name=”url” value=”jdbc:mysql://host:3306/multi1″ />
      </bean>
   </property>
   <property name=”user” value=”user”/>
   <property name=”password” value=”pwd”/>
</bean> 
 
<bean id=”dataSourceDb2″ destroy-method=”shutdown”>
   <property name=”dataSource”>
      <bean destroy-method=”shutdown”>
         <property name=”transactionManager” ref=”jotm” />
         <property name=”driverName” value=”com.mysql.jdbc.Driver” />
         <property name=”url” value=”jdbc:mysql://host:3306/multi2″ />
      </bean>
   </property>
   <property name=”user” value=”user”/>
   <property name=”password” value=”pwd”/>
</bean> 

<bean id=”t1Dao” class= “test.T1DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceDb1″ /></property>
</bean>
<bean id=”t2Dao” class= “test.T2DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceDb2″ /></property>
</bean>
<bean id=”tService” class= “test.T1ServiceImpl”>
 <property name=”t1Dao”><ref bean=”t1Dao” /></property>
 <property name=”t2Dao”><ref bean=”t2Dao” /></property> 
</bean>
<!– Configure Spring to insert JTA transaction logic for all methods –>
<bean id=”t1Service”>
 <property name=”transactionManager”><ref bean=”txManager” /></property>
 <property name=”target”><ref bean=”tService”  /></property>
 <property name=”transactionAttributes”>
  <props>
   <prop key=”*”>PROPAGATION_REQUIRED, -Exception</prop>
  </props>
 </property>
</bean> 
</beans>
T1Dao

package test;

public interface T1Dao {
 public void updateT1();
}

T1DaoImpl

package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T1DaoImpl extends JdbcDaoSupport implements T1Dao{
 public void updateT1(){
  System.out.println(” Called t1Dao.updateT1()”);
  getJdbcTemplate().update(“insert into t1(name) values(‘EFGH’)”);
 }
}
package test;

public interface T2Dao {
 public void updateT2();
}
package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T2DaoImpl extends JdbcDaoSupport implements T2Dao{
 public void updateT2(){
  System.out.println(” Called t2Dao.updateT2()”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);// here duplicate key exception will be raised and thrown back causing rollback
 }
}

T1Service
package test;

public interface T1Service {
 public void setT1Dao(T1Dao t1Dao);
 public void setT2Dao(T2Dao t2Dao);
 public void doInTransaction();
}

T1ServiceImpl

package test;

public class T1ServiceImpl implements T1Service{
 private T1Dao t1Dao;
 private T2Dao t2Dao;
 public void setT1Dao(T1Dao t1Dao){
  this.t1Dao=t1Dao;
 }
 public void setT2Dao(T2Dao t2Dao){
  this.t2Dao=t2Dao;
 }
 public void doInTransaction(){
  System.out.println(” Calling t1Dao.updateT1()”);
  t1Dao.updateT1();
  System.out.println(” Calling t2Dao.updateT2()”);
  t2Dao.updateT2();
 }
}
TestMain.java

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.T1Service;
public class TestMain {
 private static ApplicationContext appContext=new ClassPathXmlApplicationContext(new String[] {“app-context.xml”});
 public void doTrans(){
  T1Service t1Service=(T1Service)appContext.getBean(“t1Service”);
  System.out.println(” Calling t1Service.doInTransaction()”);
  t1Service.doInTransaction();
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  TestMain tm=new TestMain();
  tm.doTrans();
 }
}

hope this helps.

Disclaimer: I don’t claim to be an expert. Please send comments/corrections.

Advertisements

Read Full Post »

Create two tables in two different databases multi1,multi2 in MYSQL

DROP TABLE IF EXISTS `multi1`.`t1`;
CREATE TABLE  `multi1`.`t1` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY  USING BTREE (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `multi2`.`t2`;
CREATE TABLE  `multi2`.`t2` (
  `id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY  USING BTREE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Required jars.

mysql-connector-java-5.1.12-bin
spring-beans
spring-context
spring-context-support
spring-core
spring-jdbc
spring-tx
spring-web
spring-webmvc
jta

include jars from jotm library

xapool.jar
jotm.jar



web.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE web-app
    PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
    “http://java.sun.com/dtd/web-app_2_3.dtd“>

<web-app>
 
 <context-param>
  <param-name>contextConfigLocations</param-name>
  <param-value>
   /WEB-INF/applicationContext.xml  
  </param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
  <servlet-name>trx</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>trx</servlet-name>
  <url-pattern>*.trx</url-pattern>
 </servlet-mapping>
 
 <taglib>
   <taglib-uri>http://www.springframework.org/tags</taglib-uri&gt;
   <taglib-location>/WEB-INF/tlds/spring.tld</taglib-location>
 </taglib>

 <taglib>
  <taglib-uri>iotags</taglib-uri>
  <taglib-location>/WEB-INF/tlds/taglibs-io.tld</taglib-location>
 </taglib>
 
    <session-config>
      <session-timeout>180</session-timeout>
    </session-config>

</web-app>
applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans
  xmlns=”http://www.springframework.org/schema/beans
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>
<bean id=”jotm”  class=“org.springframework.transaction.jta.JotmFactoryBean”/>
<bean id=”txManager”
 class=”org.springframework.transaction.jta.JtaTransactionManager”>
 <property name=”userTransaction”><ref local=”jotm”/></property>
 <!– <property name=”transactionManagerName” value=”java:comp/UserTransaction” /> –>
</bean>
 

<bean id=”dataSourceJotm1″ destroy-method=”shutdown”>
   <property name=”dataSource”>
      <bean destroy-method=”shutdown”>
         <property name=”transactionManager” ref=”jotm” />
         <property name=”driverName” value=”com.mysql.jdbc.Driver” />
         <property name=”url” value=”jdbc:mysql://host:3306/multi1″ />
      </bean>
   </property>
   <property name=”user” value=”user”/>
   <property name=”password” value=”pwd”/>
</bean> 
 
<bean id=”dataSourceJotm2″ destroy-method=”shutdown”>
   <property name=”dataSource”>
      <bean destroy-method=”shutdown”>
         <property name=”transactionManager” ref=”jotm” />
         <property name=”driverName” value=”com.mysql.jdbc.Driver” />
         <property name=”url” value=”jdbc:mysql://host:3306/multi2″ />
      </bean>
   </property>
   <property name=”user” value=”user”/>
   <property name=”password” value=”pwd”/>
</bean> 

<bean id=”t1Dao” class= “test.T1DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceJotm1″ /></property>
</bean>
<bean id=”t2Dao” class= “test.T2DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceJotm2″ /></property>
</bean>
<bean id=”tService” class= “test.T1ServiceImpl”>
 <property name=”t1Dao”><ref bean=”t1Dao” /></property>
 <property name=”t2Dao”><ref bean=”t2Dao” /></property> 
</bean>
<!– Configure Spring to insert JTA transaction logic for all methods –>
<bean id=”t1Service”>
 <property name=”transactionManager”><ref bean=”txManager” /></property>
 <property name=”target”><ref bean=”tService”  /></property>
 <property name=”transactionAttributes”>
  <props>
   <prop key=”*”>PROPAGATION_REQUIRED, -Exception</prop>
  </props>
 </property>
</bean> 
</beans>

trx-servlet.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans
  xmlns=”http://www.springframework.org/schema/beans
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>

    <bean id=”viewResolver”>
        <property name=”prefix”>
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name=”suffix”>
            <value>.jsp</value>
        </property>
        <property name=”viewClass”>
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
    </bean>
  
 <bean  name=”/multi.trx”>
  <property name=”t1Service”>
            <ref bean=”t1Service”/>
       </property>
 </bean>
 
</beans>
MultiDbController

public class MultiDbController extends AbstractController {
 private T1Service t1Service;
 public void setT1Service(T1Service t1Service) {
  this.t1Service = t1Service;
 }
 protected ModelAndView handleRequestInternal(HttpServletRequest req,
   HttpServletResponse res) throws Exception {
  ModelAndView mv=new ModelAndView();
  System.out.println(” Entered into MultiDbController and calling t1Service.doInTransaction()”);
  try{
   System.out.println(” Time Started is “+System.currentTimeMillis());
   t1Service.doInTransaction();
   mv.addObject(“multidbupdateresult”,”Successfully updated the database”);
  }catch(Exception ex){
   ex.printStackTrace();
   mv.addObject(“multidbupdateresult”,ex.getMessage());
  }
  System.out.println(” Time Ended is “+System.currentTimeMillis());
  mv.setViewName(“multi”);
  return mv;
 }
}

T1Dao

package test;

public interface T1Dao {
 public void updateT1();
}

T1DaoImpl

package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T1DaoImpl extends JdbcDaoSupport implements T1Dao{
 public void updateT1(){
  System.out.println(” Called t1Dao.updateT1()”);
  getJdbcTemplate().update(“insert into t1(name) values(‘EFGH’)”);
 }
}
package test;

public interface T2Dao {
 public void updateT2();
}
package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T2DaoImpl extends JdbcDaoSupport implements T2Dao{
 public void updateT2(){
  System.out.println(” Called t2Dao.updateT2()”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);// here duplicate key exception will be raised and thrown back causing rollback
 }
}

T1Service
package test;

public interface T1Service {
 public void setT1Dao(T1Dao t1Dao);
 public void setT2Dao(T2Dao t2Dao);
 public void doInTransaction();
}

T1ServiceImpl

package test;

public class T1ServiceImpl implements T1Service{
 private T1Dao t1Dao;
 private T2Dao t2Dao;
 public void setT1Dao(T1Dao t1Dao){
  this.t1Dao=t1Dao;
 }
 public void setT2Dao(T2Dao t2Dao){
  this.t2Dao=t2Dao;
 }
 public void doInTransaction(){
  System.out.println(” Calling t1Dao.updateT1()”);
  t1Dao.updateT1();
  System.out.println(” Calling t2Dao.updateT2()”);
  t2Dao.updateT2(); 
 }
}
the multi.jsp just to display the message from model

call http://host:port/context/multi.trx

look into catalina.out/Db tables the transaction should be rolled back.

I am not sure this is the only way.

hope this helps.

Disclaimer: I don’t claim to be an expert. Please send comments/corrections

Read Full Post »

Create two tables in two different databases multi1,multi2 in MYSQL

DROP TABLE IF EXISTS `multi1`.`t1`;
CREATE TABLE  `multi1`.`t1` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY  USING BTREE (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `multi2`.`t2`;
CREATE TABLE  `multi2`.`t2` (
  `id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY  USING BTREE (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Required jars.

mysql-connector-java-5.1.12-bin
spring-beans
spring-context
spring-context-support
spring-core
spring-jdbc
spring-tx
spring-web
spring-webmvc
transactions //atomikos jars
transactions-api //atomikos jars
transactions-jdbc //atomikos jars
transactions-jta //atomikos jars
jta
atomikos-util //atomikos jars

web.xml

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE web-app
    PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
    “http://java.sun.com/dtd/web-app_2_3.dtd“>

<web-app>
 
 <context-param>
  <param-name>contextConfigLocations</param-name>
  <param-value>
   /WEB-INF/applicationContext.xml   
  </param-value>
 </context-param>
 
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <servlet>
  <servlet-name>trx</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>trx</servlet-name>
  <url-pattern>*.trx</url-pattern>
 </servlet-mapping>
 
 <taglib>
   <taglib-uri>http://www.springframework.org/tags</taglib-uri&gt;
   <taglib-location>/WEB-INF/tlds/spring.tld</taglib-location>
 </taglib>

 <taglib>
  <taglib-uri>iotags</taglib-uri>
  <taglib-location>/WEB-INF/tlds/taglibs-io.tld</taglib-location>
 </taglib>
 
    <session-config>
      <session-timeout>180</session-timeout>
    </session-config>

</web-app>
applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans
  xmlns=”http://www.springframework.org/schema/beans
  xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
  xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>
<bean
    id=”dataSourceDb1″
   class=”com.atomikos.jdbc.AtomikosDataSourceBean”
    init-method=”init”
    destroy-method=”close”>
    <property name=”uniqueResourceName” value=”dataSource1″ />
    <property name=”xaDataSourceClassName” value=”com.mysql.jdbc.jdbc2.optional.MysqlXADataSource” />
    <property name=”xaProperties”>
      <props>
        <prop key=”user”>user</prop>
        <prop key=”password”>pwd</prop>
        <prop key=”url”>jdbc:mysql://host:3306/multi1</prop>
        <prop key=”pinGlobalTxToPhysicalConnection”>true</prop>
      </props>
    </property>
  </bean>
  <bean
    id=”dataSourceDb2″
   class=”com.atomikos.jdbc.AtomikosDataSourceBean”
    init-method=”init”
    destroy-method=”close”>
    <property name=”uniqueResourceName” value=”dataSource2″ />
    <property name=”xaDataSourceClassName” value=”com.mysql.jdbc.jdbc2.optional.MysqlXADataSource” />
    <property name=”xaProperties”>
      <props>
        <prop key=”user”>root</prop>
        <prop key=”password”>root</prop>
        <prop key=”url”>jdbc:mysql://localhost:3306/multi2</prop>
        <prop key=”pinGlobalTxToPhysicalConnection”>true</prop>
      </props>
    </property>
  </bean>

<bean
    id=”atomikosTransactionManager”
   class=”com.atomikos.icatch.jta.UserTransactionManager”
    init-method=”init”
    destroy-method=”close”>
    <property name=”forceShutdown” value=”true” />
  </bean>
<!– Also use Atomikos UserTransactionImp, needed to configure Spring  –>
<bean id=”atomikosUserTransaction” class=”com.atomikos.icatch.jta.UserTransactionImp”>
    <property name=”transactionTimeout”><value>300</value></property>
</bean>

<!– Configure the Spring framework to use JTA transactions from Atomikos –>
<bean id=”springTransactionManager” class=”org.springframework.transaction.jta.JtaTransactionManager”>

 <property name=”transactionManager”><ref bean=”atomikosTransactionManager”  /></property>
 <property name=”userTransaction”><ref bean=”atomikosUserTransaction”  /></property>
</bean>
<bean id=”t1Dao” class= “test.T1DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceDb1″ /></property>
</bean>
<bean id=”t2Dao” class= “test.T2DaoImpl”>
 <property name=”dataSource”><ref bean=”dataSourceDb2″ /></property>
</bean>
<bean id=”tService” class= “test.T1ServiceImpl”>
 <property name=”t1Dao”><ref bean=”t1Dao” /></property>
 <property name=”t2Dao”><ref bean=”t2Dao” /></property> 
</bean>
<!– Configure Spring to insert JTA transaction logic for all methods –>
<bean id=”t1Service” class=”org.springframework.transaction.interceptor.TransactionProxyFactoryBean”>
 <property name=”transactionManager”><ref bean=”springTransactionManager” /></property>
 <property name=”target”><ref bean=”tService”  /></property>
 <property name=”transactionAttributes”>
  <props>
   <prop key=”*”>PROPAGATION_REQUIRED, -Exception</prop>
  </props>
 </property>
</bean> 
</beans>

MultiDbController

public class MultiDbController extends AbstractController {
 private T1Service t1Service;
 public void setT1Service(T1Service t1Service) {
  this.t1Service = t1Service;
 }
 protected ModelAndView handleRequestInternal(HttpServletRequest req,
   HttpServletResponse res) throws Exception {
  ModelAndView mv=new ModelAndView();
  System.out.println(” Entered into MultiDbController and calling t1Service.doInTransaction()”);
  try{
   System.out.println(” Time Started is “+System.currentTimeMillis());
   t1Service.doInTransaction();
   mv.addObject(“multidbupdateresult”,”Successfully updated the database”);
  }catch(Exception ex){
   ex.printStackTrace();
   mv.addObject(“multidbupdateresult”,ex.getMessage());
  }
  System.out.println(” Time Ended is “+System.currentTimeMillis());
  mv.setViewName(“multi”);
  return mv;
 }
}

T1Dao

package test;

public interface T1Dao {
 public void updateT1();
}

T1DaoImpl

package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T1DaoImpl extends JdbcDaoSupport implements T1Dao{
 public void updateT1(){
  System.out.println(” Called t1Dao.updateT1()”);
  getJdbcTemplate().update(“insert into t1(name) values(‘EFGH’)”);
 }
}
package test;

public interface T2Dao {
 public void updateT2();
}
package test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class T2DaoImpl extends JdbcDaoSupport implements T2Dao{
 public void updateT2(){
  System.out.println(” Called t2Dao.updateT2()”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);
  getJdbcTemplate().update(“insert into t2(id,name) values(1,’ABCD’)”);// here duplicate key exception will be raised and thrown back causing rollback
 }
}

T1Service
package test;

public interface T1Service {
 public void setT1Dao(T1Dao t1Dao);
 public void setT2Dao(T2Dao t2Dao);
 public void doInTransaction();
}

T1ServiceImpl

package test;

public class T1ServiceImpl implements T1Service{
 private T1Dao t1Dao;
 private T2Dao t2Dao;
 public void setT1Dao(T1Dao t1Dao){
  this.t1Dao=t1Dao;
 }
 public void setT2Dao(T2Dao t2Dao){
  this.t2Dao=t2Dao;
 }
 public void doInTransaction(){
  System.out.println(” Calling t1Dao.updateT1()”);
  t1Dao.updateT1();
  System.out.println(” Calling t2Dao.updateT2()”);
  t2Dao.updateT2();  
 }
}

jta.properties

com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.console_file_name = tm.out
com.atomikos.icatch.log_base_name = tmlog
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm
com.atomikos.icatch.console_log_level = INFO
com.atomikos.icatch.serial_jta_transactions=false

//copy the jta.properties into WEB-INF/classes

the multi.jsp just to display the message from model

call http://host:port/context/multi.trx

look into catalina.out/Db tables the transaction should be rolled back.

I am not sure this is the only way.

hope this helps.

Disclaimer: I don’t claim to be an expert. Please send comments/corrections.

Read Full Post »