Multiple databases

In the following scenario, a transaction spans two databases. An example of this scenario is a transaction that spans updates to a database maintaining the data of the customers and updates to another database managing data for the online orders of products. Both the operations, on the customer database and on the database of the orders, are a part of the same transaction.

Here is an example of a distributed transaction--multiple databases:

Now, we will see a simple test for distributed transactions with multiple databases. Here, we will take two DataSources:

    <xa-datasource jndi-name="java:/jboss/datasources/texts" pool-name="texts">        
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
<xa-datasource-property name="URL">jdbc:h2:mem:texts</xa-datasource-property>
...

We will also use the popular ExampleDS. It has a XA configuration by default. Now, take two persistent units that point to the two DataSources:

<persistence-unit name="mainTexts">
<jta-data-source>
java:jboss/datasources/texts
</jta-data-source>
...
<persistence-unit name="mainImages">
<jta-data-source>
java:jboss/datasources/ExampleDS
</jta-data-source>
...

Now, create an entity for the mainTexts persistent unit:

@PersistenceUnit(unitName="mainTexts")
@Entity
public class Text {
private Format format;
private String value;
...
public Text(Format format, String value) {
this.format = format;
this.value = value;
}
...

Also, create an entity for the mainImages persistent unit:

@PersistenceUnit(unitName="mainImages")
@Entity
public class Image {
...
private String name;
private byte[] file;
public Image(String name, byte[] file) {
this.name = name;
this.file = file;
}
...

These entities can be managed by two stateless:

@Stateless
public class Images implements Magazine<Image> {
@PersistenceContext(unitName="mainImages")
private EntityManager images;
@Override
public void write(Image element) {
images.persist(element);
}
@Override
public List<Image> read() {
return images.createQuery("from Image").getResultList();
}
}

And:

@Stateless
public class Texts implements Magazine<Text> {
@PersistenceContext(unitName="mainTexts")
private EntityManager texts;
@Override
public void write(Text element) {
texts.persist(element);
}
@Override
public List<Text> read() {
return texts.createQuery("from Text").getResultList();
}
}

Here is the common interface:

public interface Magazine<T> {
void write(T element);
List<T> read();
}

Now, we can execute some inserts from the two databases:

@Inject
private UserTransaction userTransaction;
@EJB(mappedName = "java:module/Images")
private Magazine<Image> images;
@EJB(mappedName = "java:module/Texts")
private Magazine<Text> texts;
...
userTransaction.begin();
images.write(new Image("image_title_1.jpg", "First title".getBytes()));
texts.write(new Text(BOLD, "starting first chapter"));
texts.write(new Text(ITALIC, "starting first paragraph"));
images.write(new Image("image_title_2.jpg", "Second title".getBytes()));
texts.write(new Text(BOLD, "starting second chapter"));
texts.write(new Text(ITALIC, "starting first paragraph"));
images.write(new Image("image_title_1.jpg", "Third title".getBytes()));
texts.write(new Text(BOLD, "starting third chapter"));

Now, we end with rollback:

userTransaction.rollback();

We can also start some query from the two databases:

images.read().size();
texts.read().size();

The rollback was executed automatically in both databases!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.22.107