001 // Copyright 2007, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.tynamo.jpa;
016
017 import javax.persistence.EntityManager;
018
019 /**
020 * Manages the JPA transaction for the current thread. This includes creating an entity manager as
021 * needed, allowing the em to checkpoint (commit the current transaction and continue) and commit
022 * the transaction automatically at the end of the request.
023 * <p/>
024 * Remember that in Tapestry, action requests and render requests are entirely separate, and you
025 * will see a separate request and a separate transaction for each. Care should be taken to ensure
026 * that entity objects that are retained (in the session, as persistent field values) between
027 * requests are handled correctly (they tend to become detached instances).
028 * <p/>
029 * This implementation of this service is per-thread.
030 */
031 public interface JPATransactionManager
032 {
033 /**
034 * Gets the active session for this request, creating it as necessary. When the session is first
035 * created, a transaction is started.
036 *
037 * @return the request's entityManager
038 * @see JPAEntityManagerSource
039 */
040 EntityManager getEntityManager();
041
042 /**
043 * Commits the current transaction (which will cause a flush of data to the database), then
044 * starts a new transaction to replace it.
045 */
046 void commit();
047
048 /**
049 * Aborts the current transaction, and starts a new transaction to replace it.
050 */
051 void abort();
052 }