我有一个实用程序类,如下所示。 我想能够将此类与RESOURCE_LOCAL或JTA持久性单元一起使用。 如果我将persistence.xml从JTA更改为RESOURCE_LOCAL,则不应更改代码。
我尝试使用EntityManager.getTransaction()查看是否存在活动事务,但是如果使用JTA,则调用getTransaction()会引发异常。 我可以用try / catch包围对getTransaction()的调用,但我不想为此使用例外处理。 EntityManager.getProperties()没有显示任何指示JTA或RESOURCE_LOCAL的内容。
我需要某种方式来告诉下面的代码中EntityManager(或EntityManagerFactory)正在使用什么类型的持久性单元。
我尝试使用EntityManager.getTransaction()查看是否存在活动事务,但是如果使用JTA,则调用getTransaction()会引发异常。 我可以用try / catch包围对getTransaction()的调用,但我不想为此使用例外处理。 EntityManager.getProperties()没有显示任何指示JTA或RESOURCE_LOCAL的内容。
我需要某种方式来告诉下面的代码中EntityManager(或EntityManagerFactory)正在使用什么类型的持久性单元。
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(); ...