// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package tx.nonxadb.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Bad extends DB {
	private static EntityManagerFactory emf= Persistence.createEntityManagerFactory("mysql");
	@Override
	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}
	@Override
	public void test() {
		for(int i = 0; i < 10; i++) {
			EntityManager em = getEntityManager();
	        try {
	    		em.getTransaction().begin();
	            TblSrc osrc = em.find(TblSrc.class, 1);
	            osrc.setVal(osrc.getVal() - 1);
	            em.getTransaction().commit();
	            if(i % 2 != 0) throw new Problem();
	    		em.getTransaction().begin();
	            TblTrg otrg = em.find(TblTrg.class, 1);
	            otrg.setVal(otrg.getVal() + 1);
	            em.getTransaction().commit();
	        } catch(Problem ex) {
	            System.out.println("Problem");
	        }
	        em.close();
		}
	}
	public static void main(String[] args) {
		demo(new Bad());
		emf.close();
	}
}
