EJB3.0 做一个项目,用的拦截器管理事务,当第一次提交保存时,不会报错,但第二次提交保存要报事务被其它事务使用的错误?但在拦截器中,我已经commit()了的啊?怎么会占用呢?如果第二次提交的是更新操作,就不会报错?找了两天,莫法了,求各位高手了,给小弟指条明路!
听说:默认作用的是乐观锁的嘛!我改成CMT也是同样的情况!CustInterceptor 类
红色为事务部分@TransactionManagement(TransactionManagementType.BEAN)
public class CustInterceptor { @Resource
private UserTransaction userTransaction;

@EJB
TCustinfoFacadeLocal custinfoFacadeLocal;

private TCustinfo custinfo = null;

@AroundInvoke
public Object interceptorMethod(InvocationContext ic) throws Exception {
Message message = (Message) ic.getParameters()[0];
String xml = ScmcsUtil.bytesMessage2String(message);
Head basicInfor=ScmcsUtil.Xml2Object(xml, Head.class);
custinfo=custinfoFacadeLocal.findById(basicInfor.getPartyId());
if (custinfo != null) {

                            userTransaction.begin();
Object object = ic.proceed();
userTransaction.commit();
                            

                            return object;
}else {
LogUtil.log(xml, Priority.ERROR, new RecordNotExistException("客户号为:"
+ basicInfor.getPartyId() + "的客户不存在,请先开户"));
return null;
}
}

}persistence.xml 配置文件
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0"> <persistence-unit name="SCMS_EJB30v10PU" transaction-type="JTA">
<jta-data-source>jndiDB2</jta-data-source>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.DB2Dialect" />
<!--property name="hibernate.hbm2ddl.auto" value="update" /-->
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit></persistence>IndiLiabilityInfoMDB 消息驱动了BEAN@Interceptors( { SystemInterceptor.class, CustInterceptor.class })
@TransactionManagement(TransactionManagementType.BEAN)
@MessageDriven(mappedName = "jms.synTopic.IndiLiabilityInfo", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "IndiLiabilityInfoMDB"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "IndiLiabilityInfoMDB") })
public class IndiLiabilityInfoMDB implements MessageListener {     private TFamAsset famAsset = null;
     public void onMessage(Message message) {
famAsset = new TFamAsset();
famAsset.setCrtDate(DateUtil.getStringByDate(indiLiabilityInfo.getUpdatedTs()));
famAssetFacadeLocal.save(famAsset);
     }

解决方案 »

  1.   

    TFamAssetFacade 实现类
    用MyEclipse自动生成的package com.computech.dao.impl;import com.computech.dao.TFamAssetFacadeLocal;
    import com.computech.po.TFamAsset;
    import com.computech.util.LogUtil;
    import java.util.List;
    import java.util.logging.Level;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.FlushModeType;
    import javax.persistence.LockModeType;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;import org.apache.log4j.Priority;/**
     * Facade for entity TFamAsset.
     * 
     * @see com.computech.po.TFamAsset
     * @author MyEclipse Persistence Tools
     */
    @Stateless
    public class TFamAssetFacade implements TFamAssetFacadeLocal {
    // property constants
           …………
    @PersistenceContext
    private EntityManager entityManager;
    public void save(TFamAsset entity) {
    LogUtil.log("saving TFamAsset instance", Priority.INFO, null);
    try {
    entityManager.persist(entity);
    LogUtil.log("save successful", Priority.INFO, null);
    } catch (RuntimeException re) {
    LogUtil.log("save failed", Priority.ERROR, re);
    throw re;
    }
    } public void delete(TFamAsset entity) {
    LogUtil.log("deleting TFamAsset instance", Priority.INFO, null);
    try {
    entity = entityManager.getReference(TFamAsset.class, entity
    .getRecdNo());
    entityManager.remove(entity);
    LogUtil.log("delete successful", Priority.INFO, null);
    } catch (RuntimeException re) {
    LogUtil.log("delete failed", Priority.ERROR, re);
    throw re;
    }
    } public TFamAsset update(TFamAsset entity) {
    LogUtil.log("updating TFamAsset instance", Priority.INFO, null);
    try {
    TFamAsset result = entityManager.merge(entity);
    LogUtil.log("update successful", Priority.INFO, null);
    return result;
    } catch (RuntimeException re) {
    LogUtil.log("update failed", Priority.ERROR, re);
    throw re;
    }
    } public TFamAsset findById(Integer id) {
    LogUtil.log("finding TFamAsset instance with id: " + id, Priority.INFO,
    null);
    try {
    TFamAsset instance = entityManager.find(TFamAsset.class, id);
    return instance;
    } catch (RuntimeException re) {
    LogUtil.log("find failed", Priority.ERROR, re);
    throw re;
    }
    } /**
     * Find all TFamAsset entities with a specific property value.
     * 
     * @param propertyName
     *            the name of the TFamAsset property to query
     * @param value
     *            the property value to match
     * @return List<TFamAsset> found by query
     */
    @SuppressWarnings("unchecked")
    public List<TFamAsset> findByProperty(String propertyName,
    final Object value) {
    LogUtil.log("finding TFamAsset instance with property: " + propertyName
    + ", value: " + value, Priority.INFO, null);
    try {
    final String queryString = "select model from TFamAsset model where model."
    + propertyName + "= :propertyValue";
    Query query = entityManager.createQuery(queryString);
    query.setParameter("propertyValue", value);
    return query.getResultList();
    } catch (RuntimeException re) {
    LogUtil.log("find by property name failed", Priority.ERROR, re);
    throw re;
    }
    } public List<TFamAsset> findByCustno(Object custno) {
    return findByProperty(CUSTNO, custno);
    } public List<TFamAsset> findByHouseArea(Object houseArea) {
    return findByProperty(HOUSE_AREA, houseArea);
    } public List<TFamAsset> findByHouseValue(Object houseValue) {
    return findByProperty(HOUSE_VALUE, houseValue);
    } public List<TFamAsset> findByHouseRe(Object houseRe) {
    return findByProperty(HOUSE_REMARK, houseRe);
    } public List<TFamAsset> findByMachineNum(Object machineNum) {
    return findByProperty(MACHINE_NUM, machineNum);
    } public List<TFamAsset> findByMachineValue(Object machineValue) {
    return findByProperty(MACHINE_VALUE, machineValue);
    } public List<TFamAsset> findByMachineRe(Object machineRe) {
    return findByProperty(MACHINE_REMARK, machineRe);
    } public List<TFamAsset> findByConsumableNum(Object consumableNum) {
    return findByProperty(CONSUMABLE_NUM, consumableNum);
    } public List<TFamAsset> findByConsumableValue(Object consumableValue) {
    return findByProperty(CONSUMABLE_VALUE, consumableValue);
    } public List<TFamAsset> findByConsumableRe(Object consumableRe) {
    return findByProperty(CONSUMABLE_REMARK, consumableRe);
    } public List<TFamAsset> findByToolNum(Object toolNum) {
    return findByProperty(TOOL_NUM, toolNum);
    } public List<TFamAsset> findByToolValue(Object toolValue) {
    return findByProperty(TOOL_VALUE, toolValue);
    } public List<TFamAsset> findByToolRe(Object toolRe) {
    return findByProperty(TOOL_REMARK, toolRe);
    } public List<TFamAsset> findByCoinValue(Object coinValue) {
    return findByProperty(COIN_VALUE, coinValue);
    } public List<TFamAsset> findByCoinRe(Object coinRe) {
    return findByProperty(COIN_REMARK, coinRe);
    } public List<TFamAsset> findByBeastNum(Object beastNum) {
    return findByProperty(BEAST_NUM, beastNum);
    } public List<TFamAsset> findByBeastValue(Object beastValue) {
    return findByProperty(BEAST_VALUE, beastValue);
    } public List<TFamAsset> findByBeastRe(Object beastRe) {
    return findByProperty(BEAST_REMARK, beastRe);
    } public List<TFamAsset> findByCreditorValue(Object creditorValue) {
    return findByProperty(CREDITOR_VALUE, creditorValue);
    } public List<TFamAsset> findByCreditorRe(Object creditorRe) {
    return findByProperty(CREDITOR_REMARK, creditorRe);
    } public List<TFamAsset> findByAssetValue(Object assetValue) {
    return findByProperty(ASSET_VALUE, assetValue);
    } public List<TFamAsset> findByAssetRe(Object assetRe) {
    return findByProperty(ASSET_REMARK, assetRe);
    } public List<TFamAsset> findByLoanValue(Object loanValue) {
    return findByProperty(LOAN_VALUE, loanValue);
    } public List<TFamAsset> findByLoanRe(Object loanRe) {
    return findByProperty(LOAN_REMARK, loanRe);
    } public List<TFamAsset> findByBorrowValue(Object borrowValue) {
    return findByProperty(BORROW_VALUE, borrowValue);
    } public List<TFamAsset> findByBorrowRe(Object borrowRe) {
    return findByProperty(BORROW_REMARK, borrowRe);
    } public List<TFamAsset> findBySponsionValue(Object sponsionValue) {
    return findByProperty(SPONSION_VALUE, sponsionValue);
    } public List<TFamAsset> findBySponsionRe(Object sponsionRe) {
    return findByProperty(SPONSION_REMARK, sponsionRe);
    } public List<TFamAsset> findByOwesValue(Object owesValue) {
    return findByProperty(OWES_VALUE, owesValue);
    } public List<TFamAsset> findByOwesRe(Object owesRe) {
    return findByProperty(OWES_REMARK, owesRe);
    } public List<TFamAsset> findByInbankCd(Object inbankCd) {
    return findByProperty(INBANK_CD, inbankCd);
    } public List<TFamAsset> findByUserId(Object userId) {
    return findByProperty(USER_ID, userId);
    } public List<TFamAsset> findByCrtDate(Object crtDate) {
    return findByProperty(CRT_DATE, crtDate);
    } public List<TFamAsset> findByLstModDt(Object lstModDt) {
    return findByProperty(LST_MOD_DT, lstModDt);
    } /**
     * Find all TFamAsset entities.
     * 
     * @return List<TFamAsset> all TFamAsset entities
     */
    @SuppressWarnings("unchecked")
    public List<TFamAsset> findAll() {
    LogUtil.log("finding all TFamAsset instances", Priority.INFO, null);
    try {
    final String queryString = "select model from TFamAsset model";
    Query query = entityManager.createQuery(queryString);
    return query.getResultList();
    } catch (RuntimeException re) {
    LogUtil.log("find all failed", Priority.ERROR, re);
    throw re;
    }
    }}
      

  2.   

    异常信息:
     <Warning> <EJB> <BEA-010065> <MessageDrivenBean threw an Exception in onMessage(). The exception was:
     java.lang.reflect.UndeclaredThrowableException.
    java.lang.reflect.UndeclaredThrowableException
    at $Proxy103.onMessage(Unknown Source)
    at weblogic.ejb.container.internal.MDListener.execute(MDListener.java:466)
    at weblogic.ejb.container.internal.MDListener.run(MDListener.java:744)
    at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:516)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    Truncated. see log file for complete stacktrace
    weblogic.transaction.RollbackException: Optimistic locking errors were detected when flushing to the data store.  The following objects may have been concurrently modified in another transaction: [com.computech.po.TFamAsset-6976171]
    at weblogic.transaction.internal.TransactionImpl.throwRollbackException(TransactionImpl.java:1818)
    at weblogic.transaction.internal.ServerTransactionImpl.internalCommit(ServerTransactionImpl.java:336)
    at weblogic.transaction.internal.ServerTransactionImpl.commit(ServerTransactionImpl.java:230)
    at weblogic.transaction.internal.TransactionManagerImpl.commit(TransactionManagerImpl.java:283)
    at weblogic.transaction.internal.TransactionManagerImpl.commit(TransactionManagerImpl.java:277)
    Truncated. see log file for complete stacktrace
    <openjpa-1.1.0-r422266:657916 nonfatal store error> org.apache.openjpa.util.OptimisticException: Optimistic locking errors were detected when flushing to the data store.  The following objects may have been concurrently modified in another transaction: [com.computech.po.TFamAsset-6976171] at org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2157)
    at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2007)
    at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1905)
    at org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1823)
    at weblogic.transaction.internal.ServerSCInfo.doBeforeCompletion(ServerSCInfo.java:1217)
    Truncated. see log file for complete stacktrace
    <openjpa-1.1.0-r422266:657916 nonfatal store error> org.apache.openjpa.util.OptimisticException: An optimistic lock violation was detected when flushing object instance "com.computech.po.TFamAsset-6976171" to the data store.  This indicates that the object was concurrently modified in another transaction.
    FailedObject: com.computech.po.TFamAsset-6976171
    at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.populateRowManager(AbstractUpdateManager.java:160)
    at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:85)
    at org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:72)
    at org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:549)
    at org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130)
    Truncated. see log file for complete stacktrace