Dev/jta

From CubeiaWiki

Jump to: navigation, search

Contents

Firebase JTA Support

Note: This page is relevant for Firebase 1.9 or greater.

Firebase supports JTA for ordinary JDBC data sources and JPA archives. This page will briefly detail the configuration and considerations needed to enable this in games and services.

Requirements

You need an intermediate understanding of Firebase and transactional services to understand this page.

Data Sources

You can either use a full XA data source or let Firebase emulate XA functionality for you. Full XA support may be expensive, and is not supported by all drivers. In either case you deploy such data sources normally.

XA Data Source Example

This is an example of an XA data source deployment file. Properties passed to the data source are prefixed 'ds.'.

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <entry key="tx-type">LOCAL-TX</entry>
  <entry key="xa-data-source">oracle.jdbc.xa.client.OracleXADataSource</entry>
  <entry key="ds.url">jdbc:oracle:thin:@localhost:1521:XE</entry>
  <entry key="ds.password">password1</entry>
  <entry key="ds.user">user1</entry>
</properties>

Emulated XA Data Source Example

This is an example of an emulated XA data source deployment file. The 'tx-type' property is set, which indicates that this is a normal driver which should be emulated.

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <entry key="tx-type">LOCAL-TX</entry>
  <entry key="driver">com.mysql.jdbc.Driver</entry>
  <entry key="url">jdbc:mysql://localhost/test1</entry>
  <entry key="password">password1</entry>
  <entry key="user">user1</entry>
</properties>

Firebase Configuration

You need to enable JTA for the event execution per type, and you do this in the cluster properties.

# enable JTA for games (tables)
service.space.tablespace.jta-enabled=true

# enable JTA for tournaments (mtt)
service.space.mttspace.jta-enabled=true

# enable JTA for routable services
service.<public-id>.jta-enabled 

JTA can be enabled per routable service, but if you want JTA for all executing events, you can enable it globally.

# enable JTA for all events
service.jta-enabled=true 

JPA

To enable JTA for JPA you need to configure the persistence archive to use a JTA data source. You do this by pointing out the deployed data source via JNDI. Firebase mounts the data sources under 'comp/env/jdbc', so if your data source is named 'game-db' it will be accessible under this JNDI path:

java:comp/env/jdbc/game-db

You also explicitly need to configure the persistence archive to use JTA transactions.

Hibernate JTA Platform

In order for Hibernate to interact with the Firebase JTA system, you should implement a 'jta platform'. This is easy to do by extending the existing abstract base classes in Hibernate. For example:

package test;

import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;

import org.hibernate.service.jta.platform.internal.AbstractJtaPlatform;

public class FirebaseJtaPlatform extends AbstractJtaPlatform {

	private static final long serialVersionUID = 1135227572057735590L;
	
	public static final String TM_NAME = "java:comp/env/TransactionManager";
	public static final String UT_NAME = "java:comp/env/UserTransaction";

	@Override
	protected TransactionManager locateTransactionManager() {
		return (TransactionManager) jndiService().locate( TM_NAME );
	}

	@Override
	protected UserTransaction locateUserTransaction() {
		return (UserTransaction) jndiService().locate( UT_NAME );
	}
}

The above class is then configured in the persistence archive like so:

<property name="hibernate.transaction.jta.platform" value="test.FirebaseJtaPlatform" />

Example Hibernate Persistence XML

Below is an example persistence archive configuration for Hibernate, with JTA enabled using a 'game-db' data source (which must be an XA or an emulated data source) for MySQL, and the above Firebase JTA platform.

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
	version="2.0">
	<persistence-unit name="game-entities" transaction-type="JTA">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<jta-data-source>java:comp/env/jdbc/game-db</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.transaction.jta.platform" value="test.FirebaseJtaPlatform" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>
</persistence>
Personal tools