001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.oozie.store; 019 020 //import javax.persistence.EntityManagerFactory; 021 022 import javax.persistence.EntityManager; 023 import javax.persistence.FlushModeType; 024 import javax.persistence.PersistenceUnit; 025 /* 026 import javax.persistence.Persistence; 027 import org.apache.oozie.CoordinatorActionBean; 028 import org.apache.oozie.CoordinatorJobBean; 029 import org.apache.oozie.WorkflowActionBean; 030 import org.apache.oozie.WorkflowJobBean; 031 import org.apache.oozie.SLAEventBean; 032 import org.apache.oozie.client.rest.JsonCoordinatorAction; 033 import org.apache.oozie.client.rest.JsonCoordinatorJob; 034 import org.apache.oozie.client.rest.JsonWorkflowAction; 035 import org.apache.oozie.client.rest.JsonWorkflowJob; 036 import org.apache.oozie.client.rest.JsonSLAEvent; 037 */ 038 import org.apache.oozie.service.Services; 039 import org.apache.oozie.service.StoreService; 040 import org.apache.oozie.util.XLog; 041 import org.apache.openjpa.persistence.OpenJPAEntityManager; 042 import org.apache.openjpa.persistence.OpenJPAPersistence; 043 044 import java.sql.Connection; 045 import java.sql.SQLException; 046 047 @PersistenceUnit(unitName = "oozie") 048 /** 049 * <code>Store</code> Abstract class to separate Entities from Actual store implementation 050 */ 051 public abstract class Store { 052 053 private EntityManager entityManager; 054 055 /** 056 * create a fresh transaction 057 */ 058 public Store() { 059 entityManager = Services.get().get(StoreService.class).getEntityManager(); 060 } 061 062 /** 063 * Use an existing transaction for cross store operations 064 */ 065 public Store(Store store) { 066 entityManager = store.getEntityManager(); 067 } 068 069 /** 070 * Return EntityManager 071 */ 072 public EntityManager getEntityManager() { 073 return entityManager; 074 } 075 076 /** 077 * Invoke transaction on the EntityManager 078 */ 079 public void beginTrx() { 080 entityManager.setFlushMode(FlushModeType.COMMIT); 081 entityManager.getTransaction().begin(); 082 } 083 084 /** 085 * Commit current transaction 086 */ 087 public void commitTrx() { 088 entityManager.getTransaction().commit(); 089 } 090 091 /** 092 * Close current transaction <p/> Before close transaction, it needs to be committed. 093 */ 094 public void closeTrx() { 095 entityManager.close(); 096 } 097 098 /** 099 * Rollback transaction 100 */ 101 public void rollbackTrx() { 102 entityManager.getTransaction().rollback(); 103 } 104 105 /** 106 * Check if transaction is active 107 * 108 * @return boolean 109 */ 110 public boolean isActive() { 111 return entityManager.getTransaction().isActive(); 112 } 113 114 public String getConnection() { 115 OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager); 116 Connection conn = (Connection) kem.getConnection(); 117 return conn.toString(); 118 } 119 120 public boolean isDetached(Object o) { 121 OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager); 122 return kem.isDetached(o); 123 } 124 125 public boolean isClosed() { 126 OpenJPAEntityManager kem = OpenJPAPersistence.cast(entityManager); 127 Connection conn = (Connection) kem.getConnection(); 128 try { 129 return conn.isClosed(); 130 } 131 catch (SQLException e) { 132 XLog.getLog(getClass()).info(XLog.STD, e.getMessage(), e); 133 } 134 return true; 135 } 136 137 public boolean contains(Object entity) { 138 return entityManager.contains(entity); 139 } 140 141 public String getFlushMode() { 142 return entityManager.getFlushMode().toString(); 143 } 144 }