首先,介紹一些XA有用的示例:
–如果您使用來自兩個不同persistence.xml的實體,則JPA使用兩個物理連接–這兩個連接可能需要在一個事務中提交,因此XA是您唯一的選擇
–提交數據庫更改,同時向JMS提交消息。 例如,您想保證在成功將訂單異步提交到數據庫后發送了一封電子郵件。 還有其他方法,但是JMS提供了一種事務性的方法來完成此任務,而不必考慮故障。 –由于多種政治原因(遺留系統,負責不同數據庫服務器的不同部門/不同預算)中的任何原因而寫入物理上不同的數據庫。 –請參閱http://docs.codehaus.org/display/BTM/FAQ#FAQ-WhywouldIneedatransactionmanager
因此,從我的角度來看,XA是Play需要“支持”的東西。
添加支持非常容易。 我創建了一個基于Bitronix的播放插件。 資源是在Bitronix JNDI樹中配置的(為什么Play會使用配置文件而不是JNDI ?!無論如何...)您可以使用“ withXaTransaction”啟動事務:
def someControllerMethod = Action {withXaTransaction { ctx =>TicketRepository.addValidation(user.get, bookingRef, ctx)ValidationRepository.addValidation(bookingRef, user.get, ctx)}val tickets = TicketRepository.getByEventUid(eventUid)Ok(views.html.ticketsInEvent(eventUid, getTickets(eventUid), user, eventValidationForm))}
ctx對象是XAContext(我自己的類),它使您可以查找資源(如數據源),或在發生故障時設置回滾。 因此,驗證存儲庫使用ScalaQuery(我使用“ withSession”而不是“ withTransaction!”)來完成此操作:
def addValidation(bookingRef: String, validator: User, ctx: XAContext) = {val ds = ctx.lookupDS("jdbc/maxant/scalabook_admin")Database.forDataSource(ds) withSession { implicit db: Session =>Validations.insert(Validation(bookingRef, validator.email, new java.sql.Timestamp(now)))}}
票務回購使用JMS執行以下操作:
def addValidation(user: User, bookingRef: String, ctx: XAContext) = {val xml = {bookingRef}{user.email}val qcf = ctx.lookupCF("jms/maxant/scalabook/ticketvalidations")val qc = qcf.createConnection("ticketValidation","password")val qs = qc.createSession(false, Session.AUTO_ACKNOWLEDGE)val q = qs.createQueue("ticketValidationQueue") //val q = ctx.lookup(QUEUE).asInstanceOf[Queue]val sender = qs.createProducer(q)val m = qs.createTextMessage(xml.toString)sender.send(m)sender.closeqs.closeqc.close}
我已經通過編寫MySQL并將JMS消息發送給JBoss(HornetQ)進行了測試,它似乎運行良好(除了讓hornetQ與Bitronix一起玩是個bit子-參見此處: https ://community.jboss.org/ 線程/ 206180?tstart = 0 )。
XA支持的scala代碼為:
package ch.maxant.scalabook.play20.plugins.xasupportimport play.api.mvc.RequestHeader
import play.api.mvc.Results
import play.api.mvc.Request
import play.api.mvc.AnyContent
import play.api.mvc.Result
import play.api.mvc.Action
import play.api.mvc.Security
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import ch.maxant.scalabook.persistence.UserRepository
import bitronix.tm.TransactionManagerServices
import java.util.Hashtable
import javax.naming.Context._
import javax.naming.InitialContext
import javax.sql.DataSource
import bitronix.tm.BitronixTransaction
import java.io.File
import org.scalaquery.session.Database
import org.scalaquery.SQueryException
import scala.collection.mutable.ListBuffer
import java.sql.Connection
import java.sql.SQLException
import org.scalaquery.session.Session
import bitronix.tm.BitronixTransactionManager
import javax.jms.ConnectionFactoryclass XAContext {private val env = new Hashtable[String, String]()env.put(INITIAL_CONTEXT_FACTORY, "bitronix.tm.jndi.BitronixInitialContextFactory")private val namingCtx = new InitialContext(env);var rollbackOnly = falsedef lookup(name: String) = {namingCtx.lookup(name)}def lookupDS(name: String) = {lookup(name).asInstanceOf[DataSource]}def lookupCF(name: String) = {lookup(name).asInstanceOf[ConnectionFactory]}
}trait XASupport { self: Controller =>private lazy val tm = play.api.Play.current.plugin[XASupportPlugin] match {case Some(plugin) => plugin.tmcase None => throw new Exception("There is no XASupport plugin registered. Make sure it is enabled. See play documentation. (Hint: add it to play.plugins)")}/*** Use this flow control to make resources used inside `f` commit with the XA protocol.* Conditions: get resources like drivers or connection factories out of the context passed to f.* Connections are opened and closed as normal, for example by the withSession flow control offered * by ScalaQuery / SLICK.*/def withXaTransaction[T](f: XAContext => T): T = {tm.begin//get a ref to the transaction, in case when we want to commit we are no longer on the same thread and TLS has lost the TX.//we have no idea what happens inside f! they might spawn new threads or send work to akka asynclyval t = tm.getCurrentTransactionLogger("XASupport").info("Started XA transaction " + t.getGtrid())val ctx = new XAContext()var completed = falsetry{val result = f(ctx)completed = trueif(!ctx.rollbackOnly){Logger("XASupport").info("committing " + t.getGtrid() + "...")t.commitLogger("XASupport").info("committed " + t.getGtrid())}result}finally{if(!completed || ctx.rollbackOnly){//in case of exception, or in case of set rollbackOnly = trueLogger("XASupport").warn("rolling back (completed=" + completed + "/ctx.rollbackOnly=" + ctx.rollbackOnly)t.rollback}}}
}class XASupportPlugin(app: play.Application) extends Plugin {protected[plugins] var tm: BitronixTransactionManager = nulloverride def onStart {//TODO how about getting config out of jar!val file = new File(".", "app/bitronix-default-config.properties").getAbsolutePathLogger("XASupport").info("Using Bitronix config at " + file)val prop = System.getProperty("bitronix.tm.configuration", file) //defaultSystem.setProperty("bitronix.tm.configuration", prop) //override with default, if not set//start the TMtm = TransactionManagerServices.getTransactionManagerLogger("XASupport").info("Started TM with resource config " + TransactionManagerServices.getConfiguration.getResourceConfigurationFilename)}override def onStop {//on graceful shutdown, we want to shutdown the TM tooLogger("XASupport").info("Shutting down TM")tm.shutdownLogger("XASupport").info("TM shut down")}}
隨便使用代碼,我免費提供它:-)如果不起作用,請不要抱怨;-)
看到此插件擴展并將其轉換成更多生產版本,真是太好了。 Play更好地原生支持事務管理器,包括從JNDI中獲取資源。
祝您編程愉快,別忘了分享!
參考:來自Zoo博客The Kitchen的 JCG合作伙伴 Ant Kutschera的Play 2.0框架和XA事務 。
翻譯自: https://www.javacodegeeks.com/2012/10/play-20-framework-and-xa-transactions.html