One transaction per DAO
authorindvdum (gotoindvdum[at]gmail[dot]com)
Mon, 12 Nov 2012 05:45:11 +0400
changeset 9dc42467eb5dc
parent 8 eec474867dbe
child 10 7241826f43f6
One transaction per DAO
src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy
     1.1 --- a/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy	Sun Nov 11 03:17:06 2012 +0400
     1.2 +++ b/src/main/java/ru/indvdum/jpa/dao/JPADataAccessObject.groovy	Mon Nov 12 05:45:11 2012 +0400
     1.3 @@ -35,9 +35,29 @@
     1.4  	protected static String persistenceUnitName = null;
     1.5  	protected static EntityManagerFactory emf = Persistence.createEntityManagerFactory(getPersistenceUnitName(), JPAPropertySelector.select());
     1.6  	protected EntityManager em = emf.createEntityManager();
     1.7 +	protected EntityTransaction tx = null;
     1.8  
     1.9  	JPADataAccessObject() {
    1.10 -		em.isOpen();
    1.11 +		tx = em.getTransaction();
    1.12 +		tx.begin();
    1.13 +	}
    1.14 +
    1.15 +	void close() {
    1.16 +		commit()
    1.17 +		em.close()
    1.18 +		em = null
    1.19 +	}
    1.20 +
    1.21 +	void commit() {
    1.22 +		if(tx != null && tx.isActive()) {
    1.23 +			tx.commit()
    1.24 +		}
    1.25 +	}
    1.26 +
    1.27 +	void rollback() {
    1.28 +		if(tx != null && tx.isActive()) {
    1.29 +			tx.rollback()
    1.30 +		}
    1.31  	}
    1.32  
    1.33  	protected static String getPersistenceUnitName() {
    1.34 @@ -69,11 +89,6 @@
    1.35  		return ds.getConnection();
    1.36  	}
    1.37  
    1.38 -	void close() {
    1.39 -		em.close()
    1.40 -		em = null
    1.41 -	}
    1.42 -
    1.43  	public <T> T mergeAndRefresh(T object) {
    1.44  		def merged = em.merge(object)
    1.45  		em.refresh(merged)
    1.46 @@ -108,11 +123,8 @@
    1.47  	}
    1.48  
    1.49  	synchronized boolean persistAndRemove(Object[] persistanceQueue, Object[] removeQueue) {
    1.50 -		EntityTransaction tx = null
    1.51  
    1.52  		try {
    1.53 -			tx = em.getTransaction()
    1.54 -			tx.begin()
    1.55  
    1.56  			if(persistanceQueue != null) {
    1.57  				for(object in persistanceQueue) {
    1.58 @@ -129,17 +141,10 @@
    1.59  					}
    1.60  				}
    1.61  			}
    1.62 -
    1.63 -			tx.commit()
    1.64  		}
    1.65  		catch(Throwable t) {
    1.66  			log.error("Error while synchronizing with Database: ", t);
    1.67 -
    1.68 -			if(tx != null && tx.isActive()) {
    1.69 -				tx.rollback()
    1.70 -			}
    1.71 -
    1.72 -			return false
    1.73 +			return false;
    1.74  		}
    1.75  
    1.76  		return true
    1.77 @@ -201,6 +206,10 @@
    1.78  		return result;
    1.79  	}
    1.80  
    1.81 +	public <T> T find(Class<T> entityClass, String field, Object value) {
    1.82 +		return find(entityClass, ["${field}": value], null);
    1.83 +	}
    1.84 +
    1.85  	public <T> List<T> list(Class<T> entityClass) {
    1.86  		CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(entityClass);
    1.87  		query.from(entityClass);