Step 01: 準備 table : TEST_GROUP & TEST_MEMBERS

 

table : TEST_GROUP

CREATE TABLE JAPPS.TEST_GROUP
(
  ID    VARCHAR2(10 BYTE),
  NAME  VARCHAR2(10 BYTE)
)

 

table :  JAPPS.TEST_MEMBERS

CREATE TABLE JAPPS.TEST_MEMBERS
(
  GID   VARCHAR2(10 BYTE),
  ID    VARCHAR2(10 BYTE),
  NAME  VARCHAR2(10 BYTE)
)

 

Step 02 : JAVA 程式段的作法

註:

1. EntityManager 的 getTransaction().begin();   不支援 //Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT

2. 使用 JpaTransactionManager 搭配 TransactionStatus 來控制

3. 只要作 JpaTransactionManager 的 commit(status) , 在 catch 中判斷  if (!status.isCompleted()) 作  baseService.getTxmBpm().rollback(status); 

package com.bpm;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.bpm.dao.japps.TestGroupDao;
import com.bpm.dao.japps.TestMembersDao;
import com.bpm.model.japps.TestGroup;
import com.bpm.model.japps.TestMembers;
import com.bpm.model.japps.TestMembersId;
import com.util.services.BaseService;

public class SingleMultiTable {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = LogManager.getLogger(SingleMultiTable.class);
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        testSingleTable();
        testMultiTable();
    }

    public static void testSingleTable(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-demo-cfg.xml");
        BaseService baseService = ctx.getBean("baseService",BaseService.class);
        TestGroupDao testGroupDao = ctx.getBean("testGroupDao", TestGroupDao.class);
        
        try {
            TestGroup testGroup = new TestGroup();
            testGroup.setId("A");
            testGroup.setName("Group 2");
            testGroupDao.save(testGroup);
            
            //testGroupDao.delete(testGroup);
        } catch (Exception e) {
            System.out.println("寫入資料庫有錯");
        }
        
    }
    
    public static void testMultiTable(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-demo-cfg.xml");
        BaseService baseService = ctx.getBean("baseService",BaseService.class);
        TestGroupDao testGroupDao = ctx.getBean("testGroupDao", TestGroupDao.class);
        TestMembersDao testMembersDao  = ctx.getBean("testMembersDao", TestMembersDao.class);
        
        TestGroup testGroup = new TestGroup();
        TestMembers testMembers = new TestMembers();
        TestMembersId testMembersId = new TestMembersId();
        
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();        
        def.setName("rootTransaction");        
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = baseService.getTxmBpm().getTransaction(def);        
        try {            
            testGroup.setId("3");
            testGroup.setName("C");
            testGroupDao.save(testGroup);
            int a;
            a = 1/0;
            testMembersId.setGid("3");
            testMembersId.setId("1");
            testMembers.setId(testMembersId);
            testMembers.setName("G3U2-2");            
            testMembersDao.save(testMembers);
            baseService.getTxmBpm().commit(status);
            
        } catch (Exception e) {
            //Session session = baseService.getSfBpmDS().getCurrentSession();
            if (!status.isCompleted()){                
                baseService.getTxmBpm().rollback(status);
            }            
            System.out.println("寫入資料庫有錯");                        
        }
        
    }

    
}


文章標籤
創作者介紹
MIS

MISTECH 技術手抄本

MIS 發表在 痞客邦 PIXNET 留言(0) 人氣()