All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.guicedee.guicedpersistence.jpa.implementations.JPAAutomatedTransactionHandler Maven / Gradle / Ivy

Go to download

A complete JPA 2.1 implementation for Standalone or EE Implementation. Enables Multiple Persistence units with full JTA Support using BTM. Perfect for Guice implementations, test suites, and Guice enabled Web Applications or EAR Projects. Requires JDK 8

There is a newer version: 62
Show newest version
package com.guicedee.guicedpersistence.jpa.implementations;


import com.oracle.jaxb21.PersistenceUnit;

import javax.persistence.EntityManager;

public class JPAAutomatedTransactionHandler
		implements com.guicedee.guicedpersistence.services.ITransactionHandler
{
	private static boolean active = false;

	private static final String RESOURCE_LOCAL_STRING = "RESOURCE_LOCAL";

	public JPAAutomatedTransactionHandler()
	{
		//No config required
	}

	/**
	 * Sets this Automated transaction handler to active
	 *
	 * @param active
	 */
	public static void setActive(boolean active)
	{
		JPAAutomatedTransactionHandler.active = active;
	}

	@Override
	public void beginTransacation(boolean createNew, EntityManager entityManager, PersistenceUnit persistenceUnit)
	{
		entityManager.getTransaction()
		             .begin();

	}

	@Override
	public void commitTransacation(boolean createNew, EntityManager entityManager, PersistenceUnit persistenceUnit)
	{
		entityManager.getTransaction()
		             .commit();
	}

	@Override
	public void setTransactionTimeout(int timeout, EntityManager entityManager, PersistenceUnit persistenceUnit)
	{

	}

	@Override
	public void rollbackTransacation(boolean createNew, EntityManager entityManager, PersistenceUnit persistenceUnit)
	{
		entityManager.getTransaction()
		             .rollback();
	}

	@Override
	public boolean transactionExists(EntityManager entityManager, PersistenceUnit persistenceUnit)
	{
		return entityManager.getTransaction()
		                    .isActive();
	}

	@Override
	public boolean active(PersistenceUnit persistenceUnit)
	{
		return active && persistenceUnit.getTransactionType() == null || persistenceUnit.getTransactionType()
		                                                                                .toString().equals(RESOURCE_LOCAL_STRING);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy