BlogJava 聯(lián)系 聚合 管理  

          Blog Stats

          隨筆檔案(17)

          文章檔案(1)


          GaoWei

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "<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();

          ?}

          }


          最后補充一點,事務是需要數(shù)據(jù)庫的支持.






          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 丹凤县| 康定县| 静宁县| 寿光市| 宣威市| 兖州市| 庆安县| 宜君县| 江西省| 海口市| 康平县| 沅江市| 集安市| 勃利县| 黔西县| 安陆市| 望谟县| 固原市| 新民市| 瑞昌市| 泰兴市| 盘山县| 利津县| 茂名市| 通山县| 斗六市| 清镇市| 上杭县| 丰镇市| 岚皋县| 泰和县| 顺平县| 云阳县| 浙江省| 浦江县| 渭源县| 安泽县| 济源市| 囊谦县| 伊春市| 鹤山市|