我想要的是使用JPA 2.0访问Websphere V8中的H2 mem数据库

时间:2015-06-17 14:26:26

标签: java jpa jdbc websphere persistence.xml

我几天来一直在努力解决这个问题,我在StackOverflow和其他开发网站上搜索了数十篇文章。

我使用Deployment Manager界面使用H2 JDBC提供程序和我的应用程序的数据源为我的应用程序配置了一个Websphere Application Server 8.0.0.3。{/ 3}}。

JDBC数据源:

Name: H2 JDBC Provider
Classpath: $(H2_JDBC_DRIVER_PATH)/h2-1.3.161.jar
Implementation class name: org.h2.jdbcx.JdbcDataSource

数据来源:

Provider: H2 JDBC Provider
Name: SGO_datasource
JNDI name: jdbc/sgodb
Use this data source in container managed persistence (CMP): true
Data store helper class name: com.ibm.websphere.rsadapter.ConnectJDBCDataStoreHelper (user-defined)
(default values for all the rest)
Custom properties:
    URL: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
    user: sa
    password: sa

我使用DM中的测试连接按钮测试了这个数据源,它工作正常。

当我尝试从我的应用程序访问此数据源时,我在首次访问时遇到此错误:

Caused by: <openjpa-2.1.2-SNAPSHOT-r422266:1227818 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.

阅读此here后,我检查了生成的WAR中persistence.xml文件的位置,它似乎位于正确的位置:WEB-INF / classes / META-INF / persistence.xml。

的persistence.xml:

<persistence version="2.0"
   xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="sgo">
      <jta-data-source>java:comp/env/jdbc/sgodb</jta-data-source>
   </persistence-unit>
</persistence>

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

发现它!

这些完整的JNDI名称似乎无法在Websphere中运行。我使用了简单的&#34; jdbc / sgodb&#34;相反,它可以找到上下文。

import android.app.LocalActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class HelloTab extends Fragment {
    private FragmentTabHost mTabHost;
    public Context context;






    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.hellotablay,container, false);
           context= getActivity().getApplicationContext();

        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
       // @SuppressWarnings("deprecation")
        LocalActivityManager mLocalActivityManager = new LocalActivityManager(this.getActivity(), false);
        mLocalActivityManager.dispatchCreate(savedInstanceState);
        mTabHost.setup(getActivity(),getChildFragmentManager(),android.R.id.tabcontent); 

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                Tab11.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
               Tab11.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                Tab11.class, null);
    enter code here

        return rootView;
    }
}

而不是

<jta-data-source>jdbc/sgodb</jta-data-source>