Maison > Questions et réponses > le corps du texte
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)
*请问大神如何解决?*
伊谢尔伦2017-04-18 09:24:41
Par exemple :
<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>
La transaction est configurée et annulée si une exception est levée.
Référence spécifique :
Instructions sur les transactions sur le site officiel : http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html
阿神2017-04-18 09:24:41
Je n'ai jamais utilisé ce produit auparavant. J'utilise généralement la configuration des transactions d'annotation de Spring. Il semble que les transactions programmatiques soient rarement utilisées de nos jours.
La méthode de configuration est probablement similaire à celle de @BodhiXuguang.