搜索

首页  >  问答  >  正文

java - ofbiz的事务如何配置才能起作用?

1.仅使用ofbiz的实体引擎,程序如下:

  boolean beganTrans = true;   
    Map<String, Object> resignMap = new HashMap<String, Object>();
    resignMap.put("userLoginId", "wwww");
    resignMap.put("password", "1321313133");
    resignMap.put("signDaySeries","0");
    resignMap.put("isValidate", "1");
    Date date=new Date();
    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String buildDate=format.format(date);
    resignMap.put("regeTime", buildDate);
    GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");   

    try {      

        if (beganTrans)   
            

             beganTrans = TransactionUtil.begin();  
         
        GenericValue value=delegator.create("LotteryUser", resignMap);
     
        GenericValue value1=delegator.create("LotteryUser", resignMap);
        
        TransactionUtil.commit(beganTrans); 

                
    } catch (Exception e) {      

               e.printStackTrace();      

                  

               try {       

                        TransactionUtil.rollback(beganTrans);   

            } catch (GenericTransactionException e1) {       

                        e1.printStackTrace();   

            }   
        resp.getWriter().write("sorry");
    

}

实体引擎配置如下:

 <?xml version="1.0" encoding="UTF-8"?>
    <entity-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/entity-config.xsd">
      <resource-loader class="org.ofbiz.base.config.FileLoader"
        name="fieldfile" prefix="/framework/entity/fieldtype/" prepend-env="ofbiz.home"/>
      <transaction-factory class="org.ofbiz.geronimo.GeronimoTransactionFactory"/>
       <connection-factory class="org.ofbiz.entity.connection.DBCPConnectionFactory"/>
    
        <debug-xa-resources value="false" /> 
      <delegator distributed-cache-clear-enabled="false"
        entity-eca-reader="main" entity-group-reader="main"
        entity-model-reader="main" name="default">
        <group-map datasource-name="localmysql" group-name="org.ofbiz"/>
        <group-map datasource-name="localmysql" group-name="com.xbstar"/>
      </delegator>
      <!-- need to at least define a name for each component to use -->
      <entity-model-reader name="main"/>
      <!-- need to at least define a name for each component to use -->
      <entity-group-reader name="main"/>
      <!-- need to at least define a name for each component to use -->
      <entity-eca-reader name="main"/>
      <!-- need to at least define a name for each component to use rmp_agri -->
      <entity-data-reader name="seed"/>
      <entity-data-reader name="demo"/>
      <entity-data-reader name="ext"/>
      <field-type loader="fieldfile" location="fieldtypemysql.xml" name="mysql"/>
      <field-type loader="fieldfile" location="fieldtypeoracle.xml" name="oracle"/>
      <field-type loader="fieldfile" location="fieldtypemssql.xml" name="mssql"/>
      <datasource add-missing-on-start="true" check-on-start="true"
        field-type-name="oracle"
        helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
        join-style="ansi" name="localoracle" schema-name="BPF_NUR">
        <read-data reader-name="seed"/>
        <read-data reader-name="demo"/> 
        <read-data reader-name="ext"/>
        <inline-jdbc jdbc-driver="oracle.jdbc.driver.OracleDriver"
          jdbc-password="wxzkxbx"
          jdbc-uri="jdbc:oracle:thin:@192.168.1.154:1521:ORCL"
          jdbc-username="bpf_nur" pool-maxsize="100" pool-minsize="20"/>
      </datasource>
    
      <datasource add-missing-on-start="true" check-on-start="true"
        field-type-name="mssql"
        helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
        join-style="ansi" name="localmssql" schema-name="dbo" use-fk-initially-deferred="false">
        <read-data reader-name="seed"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <inline-jdbc isolation-level="ReadCommitted"
          jdbc-driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"
          jdbc-password="123456"
          jdbc-uri="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=rmp;SelectMethod=Cursor"
          jdbc-username="sa" pool-maxsize="100" pool-minsize="20"/>
      </datasource>
      <datasource add-missing-on-start="true" check-on-start="true"
        field-type-name="mssql"
        helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
        join-style="ansi" name="localmssql2k5" schema-name="dbo" use-fk-initially-deferred="false">
        <read-data reader-name="seed"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <inline-jdbc isolation-level="ReadCommitted"
          jdbc-driver="net.sourceforge.jtds.jdbc.Driver" jdbc-password="123"
          jdbc-uri="jdbc:jtds:sqlserver://127.0.0.1:1433/rmp_ht;charset=GBK"
          jdbc-username="sa" pool-maxsize="100" pool-minsize="20"/>
      </datasource>
      <datasource add-missing-on-start="true" check-on-start="true"
        field-type-name="mysql"
        helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
        join-style="ansi-no-parenthesis" name="localmysql"
        table-type="InnoDB" use-foreign-keys="true">
        <read-data reader-name="seed"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <inline-jdbc isolation-level="Serializable"
          jdbc-driver="com.mysql.jdbc.Driver" jdbc-password="wxzkxbx"
          jdbc-uri="jdbc:mysql://192.168.1.154:3098/lotser?autoReconnect=true"
          jdbc-username="root" pool-maxsize="100" pool-minsize="30"/>
        <!-- <jndi-jdbc jndi-server-name="localjndi" jndi-name="java:/MySqlDataSource" isolation-level="Serializable"/> -->
    
      </datasource>
    </entity-config>



**出现的错误如下**

2016-06-29 16:23:14,439 675716 (http-0.0.0.0-8086-Processor3) [ TransactionUtil.java:349:WARN ]
---- exception report ----------------------------------------------------------
[TransactionUtil.setRollbackOnly] Calling transaction setRollbackOnly; this stack trace shows where this is happening:
Exception: java.lang.Exception
Message: null
---- stack trace ---------------------------------------------------------------
java.lang.Exception
org.ofbiz.entity.transaction.TransactionUtil.setRollbackOnly(TransactionUtil.java:349)
org.ofbiz.entity.transaction.TransactionUtil.rollback(TransactionUtil.java:286)
org.ofbiz.entity.GenericDelegator.create(GenericDelegator.java:723)
org.ofbiz.entity.GenericDelegator.create(GenericDelegator.java:656)
com.xbstar.wei.servlet.UserLogin.doGet(UserLogin.java:130)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:548)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)

java.lang.Thread.run(Unknown Source)

           *请问大神如何解决?* 
天蓬老师天蓬老师2780 天前843

全部回复(2)我来回复

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:24:41

    举个列子:

    <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        </bean>
        
        <tx:annotation-driven transaction-manager="transactionManager" />
    
        <context:annotation-config />    
            <tx:advice id="defaultTxAdvice">
            <tx:attributes>
                <tx:method name="*" rollback-for="Exception" />
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="ao_bo"
                expression="(execution(* *..*BoImpl.*(..))) or ( execution(* *..*AoImpl.*(..)) and ( not execution(* *..*AoImpl.mtx_*(..)) ) )" />
            <aop:advisor pointcut-ref="ao_bo" advice-ref="defaultTxAdvice" />
        </aop:config>
    

    配置了事务,抛异常就回滚。

    具体参考:
    官网中关于事务的说明:http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html

    回复
    0
  • 阿神

    阿神2017-04-18 09:24:41

    没用过这货,一般都是用Spring的注解式事务配置,貌似现在也较少见到用编程式事务了。

    配置方式大概跟@菩提旭光 的差不多。

    回复
    0
  • 取消回复