I have a utility class as shown below. I want to be able to use this class with RESOURCE_LOCAL or JTA storage units. If I change persistence.xml from JTA to RESOURCE_LOCAL, I will not need to change the code.
I tried using EntityManager.getTransaction () to find out if there is an active transaction, but calling getTransaction () throws an exception if using JTA. I could combine the getTransaction () call with try / catch, but I don't want to resort to exception handling for this. EntityManager.getProperties () does not show anything that points to JTA or RESOURCE_LOCAL
I need to somehow indicate which type of continuity unit the EntityManager (or EntityManagerFactory) uses in the code below.
public class CredentialsUtil {
public static final String VGBD_PU = "VGDBpu";
static Logger logger = Logger.getLogger(CredentialsUtilStatic.class);
static EntityManagerFactory emf = Persistence.createEntityManagerFactory(VGBD_PU);
public static final String sharedKey="pgpsympwd";
public static String getPassword(String username) {
String selectStr = "select pgp_sym_decrypt(pgpsympassword, '" + sharedKey + "') from credentials where username='" + username + "'";
EntityManager em =null;
String password = "";
try {
em = emf.createEntityManager();
java.util.Map<java.lang.String,java.lang.Object> propMap = em.getProperties();
logger.info(propMap.keySet().size() + " properties");
for (String key : propMap.keySet())
logger.info(key + ", " + propMap.get(key));
EntityTransaction tx = em.getTransaction(); ...