Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 如何判断我的EntityManager使用的是JTA还是RESOURCE_本地数据源?_Jpa_Jta_Entitymanager - Fatal编程技术网

Jpa 如何判断我的EntityManager使用的是JTA还是RESOURCE_本地数据源?

Jpa 如何判断我的EntityManager使用的是JTA还是RESOURCE_本地数据源?,jpa,jta,entitymanager,Jpa,Jta,Entitymanager,我有一个实用程序类,如下所示。我希望能够将这个类与RESOURCE_LOCAL或JTA持久性单元一起使用。如果我将persistence.xml从JTA更改为RESOURCE_LOCAL,我就不必更改代码 我尝试使用EntityManager.getTransaction()查看是否存在活动事务,但如果使用JTA,则对getTransaction()的调用会引发异常。我可以用try/catch来包围对getTransaction()的调用,但我不想为此求助于异常处理。EntityManager.

我有一个实用程序类,如下所示。我希望能够将这个类与RESOURCE_LOCAL或JTA持久性单元一起使用。如果我将persistence.xml从JTA更改为RESOURCE_LOCAL,我就不必更改代码

我尝试使用EntityManager.getTransaction()查看是否存在活动事务,但如果使用JTA,则对getTransaction()的调用会引发异常。我可以用try/catch来包围对getTransaction()的调用,但我不想为此求助于异常处理。EntityManager.getProperties()不显示任何指示JTA或本地资源的内容

我需要一些方法来告诉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(); ...
public class CredentialsUtil{
公共静态最终字符串VGBD_PU=“VGDBpu”;
静态记录器Logger=Logger.getLogger(CredentialsUtilStatic.class);
静态EntityManagerFactory emf=Persistence.createEntityManagerFactory(VGBD_PU);
公共静态最终字符串sharedKey=“pgpsympwd”;
公共静态字符串getPassword(字符串用户名){
String selectStr=“select pgp_sym_decrypt(pgpsympassword,“+sharedKey+”)从用户名=“+username+””的凭据中解密;
EntityManager em=null;
字符串密码=”;
试一试{
em=emf.createEntityManager();
java.util.Map propMap=em.getProperties();
logger.info(propMap.keySet().size()+属性);
for(字符串键:propMap.keySet())
logger.info(key+,“+propMap.get(key));
EntityTransaction tx=em.getTransaction()。。。

您可以尝试类似的方法,这依赖于两种实体管理器类型的不同事务API:

public boolean isResourceLocal(EntityManager em) {

  try {
    EntityTransaction tx = em.getTransaction();
    return true;
  } catch (IllegalStateException ex) {
    return false;
  }

}

在JTA实体管理器上调用
getTransaction()
时,持久性提供程序抛出
IllegalStateException

详情请参阅:

EntityTransaction getTransaction()

返回资源级EntityTransaction对象。EntityTransaction实例可串行用于开始和提交多个事务

返回: EntityTransaction实例

抛出: IllegalStateException-如果在JTA实体管理器上调用

使用EclipseLink时,会导致当前事务回滚:

public EntityTransaction getTransaction() {
    try {
        return ((TransactionWrapper)this.transaction).getTransaction();
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    }
}

由于JPA2.1,您可以使用em.isJoinedTransaction()