1

调用我的 Web 服务时出现以下错误:

org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.

Webservice 类基本上是尝试使用 JPA 插入数据,并且基于上述错误消息,EntityManager 似乎无法访问数据源条目,如下所示:

<persistence-unit name="TEST_P" transaction-type="RESOURCE_LOCAL"> 
        <provider>
        com.ibm.websphere.persistence.PersistenceProviderImpl
        </provider>
        <non-jta-data-source>java:comp/env/jdbc/DATA</non-jta-data-source>
        <class>jpa.WSTGHandler</class>
            <properties>

        </properties>
    </persistence-unit>

我还在 web.xml 中定义了数据源条目,如下所示:

      <resource-ref>
        <res-ref-name>java:comp/env/jdbc/DATA</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

在 In Servlet 中嵌入 JPA 代码可以成功定位数据源。我怎样才能让 Web 服务以同样的方式工作?

PS:我使用的是 Websphere 7、JPA 1.2 版和 JAX-WS

根据要求,在 Web 服务方法中调用以下类,即 beginTransaction()

public class EntityManagerHelper {

    private static final EntityManagerFactory emf; 
    private static final ThreadLocal<EntityManager> threadLocal;
    private static final Logger logger;

    static {
        emf = Persistence.createEntityManagerFactory("TEST_P");         
        threadLocal = new ThreadLocal<EntityManager>();
        logger = Logger.getLogger("TEST_P");
        logger.setLevel(Level.FINE);
    }

    public static EntityManager getEntityManager() {
        EntityManager manager = threadLocal.get();      
        if (manager == null || !manager.isOpen()) {
            manager = emf.createEntityManager();
            threadLocal.set(manager);
        }
        return manager;
    }

     public static void closeEntityManager() {
        EntityManager em = threadLocal.get();
        threadLocal.set(null);
        if (em != null) em.close();
    }

    public static void beginTransaction() {
        getEntityManager().getTransaction().begin();
    }

    public static void commit() {
        getEntityManager().getTransaction().commit();
    }  

    public static void rollback() {
        getEntityManager().getTransaction().rollback();
    } 

    public static Query createQuery(String query) {
        return getEntityManager().createQuery(query);
    }

    public static void log(String info, Level level, Throwable ex) {
        logger.log(level, info, ex);
    }

}
4

1 回答 1

0

问题解决了。这只是 res-ref-name 中的一个错字:)

于 2012-04-17T17:46:12.870 回答