码迷,mamicode.com
首页 > 其他好文 > 详细

wildfly jobss 同时连接多个数据源

时间:2016-04-19 17:14:22      阅读:850      评论:0      收藏:0      [点我收藏+]

标签:

由于需要从一个远程机器取数据。处理后保存到本地数据库处理。用 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即可。

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 同时连接多个数据源

标签:

原文地址:http://www.cnblogs.com/bigben0123/p/5408486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!