MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleEMTestCase.java
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 package com.mysql.clusterj.jpatest;
20 
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityTransaction;
23 
27 public abstract class SingleEMTestCase extends SingleEMFTestCase {
28 
29  protected EntityManager em;
30 
31  protected EntityTransaction tx;
32 
33  @Override
34  public void setUp() {
35  super.setUp();
36  em = emf.createEntityManager();
37  }
38 
39  @Override
40  public void setUp(Object... props) {
41  super.setUp(props);
42  em = emf.createEntityManager();
43  }
44 
45  @Override
46  public void tearDown() throws Exception {
47  rollback();
48  close();
49  super.tearDown();
50  }
51 
56  protected boolean begin() {
57  EntityTransaction tx = em.getTransaction();
58  if (tx.isActive())
59  return false;
60  tx.begin();
61  return true;
62  }
63 
68  protected boolean commit() {
69  EntityTransaction tx = em.getTransaction();
70  if (!tx.isActive())
71  return false;
72 
73  tx.commit();
74  return true;
75  }
76 
81  protected boolean rollback() {
82  if (em != null && em.isOpen()) {
83  EntityTransaction tx = em.getTransaction();
84  if (!tx.isActive()) {
85  return false;
86  } else {
87  tx.rollback();
88  return true;
89  }
90  } else {
91  return false;
92  }
93  }
94 
99  protected boolean close() {
100  if (em == null)
101  return false;
102 
103  rollback();
104 
105  if (!em.isOpen())
106  return false;
107 
108  em.close();
109  return !em.isOpen();
110  }
111 
112 }