BlogJava 联系 聚合 管理  

Blog Stats

随笔档案(17)

文章档案(1)


GaoWei

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

   <bean id="insertAccount"
         class="spring.Account">
         <property name="dataSource" ref="dataSource"/>
         <property name="transactionManager" ref="transactionManager"/>

   </bean>
  
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
  
  </bean>
    <!--  连接信息配置 -->
    <bean
        id="PlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property
            name="locations"
            value="classpath:jdbc.properties"/>
    </bean>
    <bean
        id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property
            name="driverClassName"
            value="${jdbc.driver}" />
        <property
            name="username"
            value="${jdbc.user}" />
        <property
            name="password"
            value="${jdbc.password}" />
        <property
            name="url"
            value="${jdbc.url}" />
        <property
            name="initialSize"
            value="${jdbc.initialSize}" />
        <property
            name="maxActive"
            value="${jdbc.maxActive}" />
        <property
            name="maxIdle"
            value="${jdbc.maxIdle}" />
        <property
            name="maxWait"
            value="${jdbc.maxWait}" />
        <property
            name="minIdle"
            value="${jdbc.minIdle}" />
    </bean>
</beans>

package spring;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class Account
{
    private DataSource dataSource;
    private PlatformTransactionManager transactionManager;
    public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
 }

 public void sava()
    {
          new JdbcTemplate(dataSource).update("insert into useinfo values(?,?)",new Object[]{"spring","spring"}); 
    }

 public void transaction()
 {
            TransactionTemplate tt=new TransactionTemplate(getTransactionManager());
          
            tt.execute(new TransactionCallback(){
             
             public Object doInTransaction(TransactionStatus status)
             {
              
              JdbcTemplate jt=new JdbcTemplate(getDataSource());
              
              jt.update("insert into useinfo values('222','222')");
              
              jt.update("insert into useinfo values('222','?',?);");
              
              return null;
             }

    
            });
           
          
           
 }
 public void setTransactionManager(PlatformTransactionManager transactionManager) {
  this.transactionManager = transactionManager;
 }

 public PlatformTransactionManager getTransactionManager() {
  return transactionManager;
 }

 public DataSource getDataSource() {
  return dataSource;
 }
 
  
}

jdbc.driver     = oracle.jdbc.driver.OracleDriver
jdbc.url        = jdbc:oracle:thin:@192.168.1.80:1521:test
jdbc.user       =gaowei
jdbc.password   =gao123

jdbc.initialSize = 5
jdbc.maxActive  = 60
jdbc.maxIdle  = 10
jdbc.maxWait  = 50
jdbc.minIdle  = 5


package spring;

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


public class test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
  ApplicationContext ctx=new ClassPathXmlApplicationContext("spring/spring.xml");
  Account account=(Account)ctx.getBean("insertAccount");
  account.transaction();

 }

}


最后补充一点,事务是需要数据库的支持.





posted on 2006-10-15 15:46 gggg874 阅读(197) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: