由于需要从一个远程机器取数据。处理后保存到本地数据库处理。用 wildfly datasource 会报:

[com.arjuna.ats.arjuna] (default task-6) ARJUNA012140: Adding multiple last resources is disallowed. Trying to add LastResourceRecord(XAOnePhaseResource(LocalXAResourceImpl@7f19c56d[connectionListener=1......

这主要是jpa里面的的事物,只允许一个datasource连接。

采用xa-datasource即可。


另一种方案是在主控程序中禁止事务。将数据库的操作写在其他类中,使用事务。即只有要修改的操作才需要事务。

更一步,可以在远程取数据的类中,也是不需要事务的。这样就可以inject 需要使用事务的类。

主控类:

package com.italktv.iof.service;

import java.util.List;
import java.util.logging.Logger; import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Stateless;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionManagement;
import javax.inject.Inject; import com.italktv.iof.entity.AccountInfoSync; @Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
@TransactionAttribute(javax.ejb.TransactionAttributeType.NEVER)
public class SyncTableTimer { @Inject
private Logger logger; @Resource
private TimerService timerService; @Inject
IOFDbUtil iof; @Inject
private MySqlUtil mysql; // @Schedule(hour = "6", minute = "0", second = "0", persistent = false ,info = "6点执行 ")
@Schedule(hour = "*", minute = "*/1", second = "0", persistent = false, info = " ")
public void automaticCustomer() {
logger.info("=== job " + " started ====");
doTask();
logger.info("=== job end ====");
} private void cancelTimers() {
for (Timer timer : timerService.getTimers()) {
// timer.cancel();
}
} private void doTask() {
List<AccountInfoSync> list;
list = iof.getList();
while (list.size() > 0) {
logger.info(" list.size=" + list.size());
mysql.saveList(list);
list = iof.getList();
}
}
}

连接数据源1:

@Stateless
public class MySqlUtil { @Inject
private Logger logger; @PersistenceContext(unitName = "primary")
protected EntityManager em; public void saveList(List<AccountInfoSync> list) {
String dataSql = "select * " + "from profile where id=1";
Query nativeQuery = em.createNativeQuery(dataSql, MacProfile.class); MacProfile i;
try {
i = (MacProfile) nativeQuery.getSingleResult();
//
} catch (NoResultException e) {
i = new MacProfile();
} } }

连接数据源2,select操作

@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
@TransactionAttribute(javax.ejb.TransactionAttributeType.NEVER)
public class IOFDbUtil { @Inject
private Logger logger; @PersistenceContext(unitName = "theirDb")
private EntityManager theirDb; int FETCH_MAX_SIZE = 1;
int start = 0; @PostConstruct
void init() { } public List<AccountInfoSync> getList() { List<AccountInfoSync> list = null; while (true) {
String dataSql = "select * from dbtable "
+ "where id>" + start + " and id<=" + start + "+" + FETCH_MAX_SIZE;
Query nativeQuery = iof.createNativeQuery(dataSql, AccountInfoSync.class); list = (List<AccountInfoSync>) nativeQuery.getResultList();
start += FETCH_MAX_SIZE;
if (list.size() < 1 && start > maxid)
break; if (list.size() < 1) {
continue;
} for (AccountInfoSync iofsync : list) {
logger.info(iofsync.toString());
}
break;//找到就停止
}
return list;
} @Inject
MySqlUtil mm; private int getLastId() { mm.test();
return 0;
} }

  • 使用xa-datasource解决方案

standardalone.xml中:

 <subsystem xmlns="urn:jboss:domain:datasources:4.0">
<datasources>
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>sa</user-name>
<password>sa</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/statisticsystem</connection-url>
<driver>mysql</driver>
<security>
<user-name>jboss</user-name>
<password>jboss</password>
</security>
</datasource>
<datasource jndi-name="java:jboss/datasources/MySqlStatBank" pool-name="MySqlStatBank" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://211.100.75.204:5029/statistics</connection-url>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>@^#coopen</password>
</security>
</datasource>
<xa-datasource jndi-name="java:jboss/datasources/MySqlStatBank1" pool-name="MySqlStatBank1" enabled="true" use-java-context="true">
<xa-datasource-property name="ServerName">
211.100.75.204
</xa-datasource-property>
<xa-datasource-property name="PortNumber">
5029
</xa-datasource-property>
<xa-datasource-property name="DatabaseName">
statistics
</xa-datasource-property>
<driver>mysql</driver>
<xa-pool>
<min-pool-size>5</min-pool-size>
<initial-pool-size>5</initial-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
</xa-pool>
<security>
<user-name>root</user-name>
<password>@^#coopen</password>
</security>
</xa-datasource>
<drivers>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
<driver name="mysql" module="com.mysqldatabase.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>

persistent.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1"> <persistence-unit name="statLog">
<jta-data-source>java:jboss/datasources/MySqlStatBank1</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- <class>com.italktv.colnv.stat.entity.LogOfPlay</class> -->
<!-- <class>com.italktv.colnv.stat.entity.LogOfView</class> --> <properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<!-- <property name= "hibernate.hbm2ddl.auto" value ="validate" /> create-drop -->
<property name="hibernate.jdbc.fetch_size" value="15" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true"></property> <!--
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation-target" value="scripts"/>
<property name="javax.persistence.ddl-create-script-target" value="e:\createSeats.sql"/>
<property name="javax.persistence.ddl-drop-script-target" value="e:/dropSeats.sql"/>
-->
</properties> </persistence-unit> <persistence-unit name="primary" >
<class>com.italktv.colnv.stat.entity.Seat</class>
<class>com.italktv.colnv.stat.entity.SeatType</class>
<class>com.italktv.colnv.stat.entity.LogOfPlayDuration</class>
<class>com.italktv.colnv.stat.entity.ReportLiveHits</class> <properties>
<property name="hibernate.hbm2ddl.auto" value="update" />
<!-- <property name= "hibernate.hbm2ddl.auto" value ="validate" /> create-drop -->
<property name="hibernate.jdbc.fetch_size" value="15" />
<property name="hibernate.jdbc.batch_size" value="10" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true"></property> <!--
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation-target" value="scripts"/>
<property name="javax.persistence.ddl-create-script-target" value="e:\createSeats.sql"/>
<property name="javax.persistence.ddl-drop-script-target" value="e:/dropSeats.sql"/>
-->
</properties>
</persistence-unit> </persistence>

mysql driver的配置参考以前文档,在

 <driver name="mysql" module="com.mysqldatabase.mysql">

调用:
public class PlayDurationTask {

    @PersistenceContext(unitName = "statLog")
private EntityManager emStatLog; String query = "SELECT * FROM statistics.log_2016 ;";
List aa = emStatLog.createNativeQuery(query).getResultList();
//ddl语句用 createNativeUpdate
// 处理 for (int i = 0; i < aa.size(); i++) {
Object[] obj = (Object[]) aa.get(i);
// 使用obj[0],obj[1],obj[2]...取出属性 
... ...
}
} class two{

@PersistenceContext(unitName = "primary")
private EntityManager em ;
public void genLiveReport() {
em.createNativeQuery(LIVE_PLAY_USERS_BY_MAINID).executeUpdate();
logger.info(LIVE_PLAY_USERS_BY_MAINID);

}

 

参考:

Demystifying Datasource JTA and XA settings on JBoss-WildFly

One topic which is often misunderstood by middleware administrators is the configuration of JTA and XA attributesand their effect on transactions. Let's see more in practice.

Basically on a JBoss AS 6/WildFly configuration you can choose three different strategies as far as transactioons are concerned:

1) Setting jta = false and Non-XA datasource

1
<datasource jta="false"  . . . >

When setting this option you will be responsible for managing by yourself the transactions.  For example, the following code will work when using a Datasource with JTA=false:

1
2
3
4
5
6
7
8
9
10
11
12
@Resource(mappedName="java:jboss/datasources/ExampleDS"
private DataSource ds; 
. . . . . . . . . . .
Connection conn = ds.getConnection();
conn.setAutoCommit(false);
 
PreparedStatement stmt = conn.prepareStatement("INSERT into PROPERTY values (?,?)");
stmt.setString(1,key);
stmt.setString(2,value);
stmt.execute();
 
conn.commit();

Please note: the datasource with jta=false corresponds exactly to the older definition of local-tx-datasource you can find in JBoss AS 4/5

What if you are using JPA instead ?

So, when using plain JDBC you can handle by yourself transaction boundaries using commit/rollback. What about if you are using JPA with JTA=false? in short, the transaction-type for JPA will be RESOURCE_LOCAL. This would use basic JDBC-level transactions. In such scenario you are responsible for EntityManager (PersistenceContext/Cache) creating and tracking and you have to follow these rules:

  • You must use the EntityManagerFactory to get an EntityManager
  • The resulting EntityManager instance is a PersistenceContext/Cache An EntityManagerFactory can be injected via the @PersistenceUnit annotation only (not @PersistenceContext)
  • You are not allowed to use @PersistenceContext to refer to a unit of type RESOURCE_LOCAL
  • You must use the EntityTransaction API to begin/commit around every call to your EntityManger

Here is an example of using JPA with a RESOURCE_LOCAL transaction:

1
2
3
4
5
6
7
@PersistenceUnit(unitName="unit01")
private EntityManagerFactory emf;
     
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(mag);
em.getTransaction().commit();

 2) Setting jta = true and Non-XA datasource

1
<datasource jta="true"  . . . >

This is the default. When JTA is true, the JCA connection pool manager knows to enlist the connection into the JTA transaction. This means that, if the Driver and the database support it, you can use JTA transaction for a single resource.

1
2
3
4
5
6
@PersistenceContext(unitName = "unit01")
private EntityManager entityManager;
 
public void addMovie(Movie movie) throws Exception {
        entityManager.persist(movie);
}

If you try to manage JDBC transactions by yourself when jta=true an exception will be raised:

1
2
3
12:11:17,145 SEVERE [com.sample.Bean] (http-/127.0.0.1:8080-1) null: java.sql.SQLException: You cannot set autocommit during a managed transaction!
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:961)
    at org.jboss.jca.adapters.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:716)

 3) Using an XA datasource

An XA transaction, in the most general terms, is a "global transaction" that may span multiple resources. A non-XA transaction always involves just one resource.

1
<xa-datasource . . . .>

An XA transaction involves a coordinating transaction manager, with one or more databases (or other resources, like JMS) all involved in a single global transaction. Non-XA transactions have no transaction coordinator, and a single resource is doing all its transaction work itself.

The Transaction Manager coordinates all of this through a protocol called Two Phase Commit (2PC). This protocol also has to be supported by the individual resources.

In terms of datasources, an XA datasource is a data source that can participate in an XA global transaction. A non-XA datasource can't participate in a global transaction

wildfly jobss 同时连接多个数据源 datasource xa-datasource的更多相关文章

  1. wildfly jobss 同时连接多个数据源

    由于需要从一个远程机器取数据.处理后保存到本地数据库处理.用 wildfly datasource 会报: [com.arjuna.ats.arjuna] (default task-6) ARJUN ...

  2. Netbeans 中创建数据连接池和数据源步骤(及解决无法ping通问题)

    1.启动glassfish服务器, 在浏览器的地址栏中输入 http://localhost:4848 2.首先建立JDBC Connection Pools: 3.new 一个Connectio P ...

  3. jdbc基础 (五) 连接池与数据源 DBCP以及C3P0的使用

    一.连接池的概念和使用 在实际应用开发中,特别是在WEB应用系统中,如果JSP.Servlet或EJB使用JDBC直接访问数据库中的数据,每一次数据访问请求都必须经历建立数据库连接.打开数据库.存取数 ...

  4. 在Tomcat中配置连接池和数据源

    1.DataSource接口介绍 (1)DataSource 概述 JDBC1.0原来是用DriverManager类来产生一个对数据源的连接.JDBC2.0用一种替代的方法,使用DataSource ...

  5. spring boot 连接多个数据源

    在springboot中有需要连接多个数据源的情况. 首先配置文件application.properties中添加两个数据源连接字符串 mybatis.type-aliases-package=co ...

  6. Spring学习11-Spring使用proxool连接池 管理数据源

    Spring 一.Proxool连接池简介及其配置属性概述   Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是 ...

  7. 连接池、数据源、JNDI三者间的关系及用法

    连接池:连接池是由容器(比如Tomcat)提供的,用来管理池中的连接对象.连接池自动分配连接对象并对闲置的连接进行回收.连接池中的连接对象是由数据源(DataSource)创建的.连接池(Connec ...

  8. Spring使用proxool连接池 管理数据源

    一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...

  9. c3p0 连接池配置数据源

    <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledD ...

随机推荐

  1. jackjson-databind-2.9.3 笔记

    问题 客户端请求: {"skip":0,"take":10,"corpName":"","cityCode&q ...

  2. 理解使用static import 机制

    J2SE 1.5里引入了“Static Import”机制,借助这一机制,可以用略掉所在的类或接口名的方式,来使用静态成员.本文介绍这一机制的使用方法,以及使用过程中的注意事项. 在Java程序中,是 ...

  3. 牛客训练赛25-A-最长区间

    https://www.nowcoder.com/acm/contest/158#question 这题问最长的严格连续递增序列的最长长度是多少? 最开始感觉这道题不可做,因为有1e5个点,还有1e5 ...

  4. JS 实现计算一段文字中的字节数,字母数,数字数,行数,汉字数。

    看到了匹配,第一个想到了用正则表达式,哈哈,果然很方便.不过正则表达式高深莫测!我还没有研究明白啊..目前学了点皮毛.代码如下: <!DOCTYPE html PUBLIC "-//W ...

  5. 个人博客作业Week3--必应词典案例分析

    第一部分  调研,评测 (软件的bug,功能评测,黑箱测试,第8章 用户调研,12 章软件的用户体验) 下载并使用,按照描述的bug定义,找出几个功能性的比较严重的bug.至少两个.用专业的语言描述( ...

  6. WebAPI实例--第一个API

    今天终于做了第一个任务,学习API之后的第一个实例.销售设置开发API. 第一.层次结构 1.API各层 项目结构主要有五层,分别为API.BizModel.Data.DBModel.Logic. 2 ...

  7. 关于git的一些体会:

    周忠贤github链接:https://github.com/zhouzhongxian git学习心得:通过这次的学习,体会到了许多东西只要你用心去做,就没有什么做不成,,这次体会到了网上学习的重要 ...

  8. 转:为Docker容器设置固定IP实现网络联通(1)——通过Pipework为Docker容器设置

    https://blog.csdn.net/chinagissoft/article/details/51250839 1. 创建并启动一个容器: docker run --cap-add=NET_A ...

  9. [转帖] YAML 快速入门

    https://www.jianshu.com/p/97222440cd08 原始文档更加易读. YAML快速入门 叩丁狼教育 关注 2018.02.18 19:19* 字数 1776 阅读 876评 ...

  10. Linux环境(CentOS)安装维护过程中用到的常见命令

    1. yum 安装时需要选择仓库 一般的路径 /etc/repos.d/ 2. 查看安装了哪些软件的 yum list |grep docker installed 的就是已经安装的软件. 3. 卸载 ...