001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.tide.data; 022 023import javax.naming.InitialContext; 024import javax.naming.NamingException; 025import javax.transaction.NotSupportedException; 026import javax.transaction.SystemException; 027import javax.transaction.UserTransaction; 028 029import org.granite.tide.TideTransactionManager; 030 031/** 032 * Responsible for attaching a entity with the entity mangager 033 * @author cingram 034 * 035 */ 036public class JTATransactionManager implements TideTransactionManager { 037 038 public static final String USER_TRANSACTION_JNDI = "java:comp/UserTransaction"; 039 040 041 public Object begin(TideTransactionPersistenceManager pm) { 042 try { 043 InitialContext ic = new InitialContext(); 044 UserTransaction ut = (UserTransaction)ic.lookup(USER_TRANSACTION_JNDI); 045 ut.begin(); 046 return ut; 047 } 048 catch (NamingException f) { 049 throw new RuntimeException("Could not initiate JTA transaction for lazy initialization", f); 050 } 051 catch (SystemException f) { 052 throw new RuntimeException("Could not initiate JTA transaction for lazy initialization", f); 053 } 054 catch (NotSupportedException f) { 055 throw new RuntimeException("Could not initiate JTA transaction for lazy initialization", f); 056 } 057 } 058 059 public void commit(Object tx) throws Exception { 060 if (tx instanceof UserTransaction) 061 ((UserTransaction)tx).commit(); 062 } 063 064 public void rollback(Object tx) { 065 try { 066 if (tx instanceof UserTransaction) 067 ((UserTransaction)tx).rollback(); 068 } 069 catch (Exception e) { 070 throw new RuntimeException("Unable to rollback attach entity and init collection", e); 071 } 072 } 073}