2015-05-20 61 views
0

使用我的JUNIT代碼連接到Oracle數據庫時,我面臨一個問題(默認情況下它會嘗試連接到HSQL)。我使用openjpa作爲持久性提供者。我已經嘗試了很多方法,包括在persistence.xml中指定連接設置並在Test class中指定連接設置。無法使用JUNIT和OPENJPA連接到數據庫

public class CollateralHandlerTest { 

    private static InitialContext ctx; 

    @BeforeClass 
    public static void initContainer() throws Exception { 
     final Properties props = new Properties(); 
     props.put("tomee.ejbcontainer.http.port", 1800); 
     props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory"); 
     props.put("ProvAppCLJPA.openjpa.ConnectionURL", "jdbc:oracle:thin:@localhost:1521:XE"); 
     props.put("ProvAppCLJPA.openjpa.ConnectionPassword", "XXXX"); 
     props.put("ProvAppCLJPA.openjpa.ConnectionDriverName", "oracle.jdbc.OracleDriver"); 
     props.put("ProvAppCLJPA.openjpa.ConnectionUserName", "PROVAPPCL_DE"); 
     ctx = new InitialContext(props); 

    } 

    @Test 
    public void testCollateralHandler() throws NamingException, ProvAppException { 
     Object object = ctx.lookup("CollateralHandlerLocal"); 
     assertTrue(object instanceof ICollateralHandler); 
     ICollateralHandler collHandler = (ICollateralHandler) ctx.lookup("CollateralHandlerLocal"); 
     List<Customer> cust = collHandler.getAllCollateral("XXXX"); 
    } 
} 

我而在「getAllCollat​​eral」功能執行

final Query query = entityManager.createNamedQuery(""); 
    query.getResultList(); 

獲得在測試方法的最後一行的錯誤。

我得到下面的異常

<openjpa-2.2.0-r422266:1244990 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "null" and URL "null". You may have specified an invalid URL. 
     at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:255) 
     at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:241) 
    --Cut Short 
    Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (invalid authorization specification - not found: SA) 
     at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549) 
     at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388) 
    --Cut Short 
    Caused by: java.sql.SQLInvalidAuthorizationSpecException: invalid authorization specification - not found: SA 
     at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
     at org.hsqldb.jdbc.Util.sqlException(Unknown Source) 
     at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source) 
     at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source) 
     at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source) 
     --Cut Short 
    Caused by: org.hsqldb.HsqlException: invalid authorization specification - not found: SA 
     at org.hsqldb.error.Error.error(Unknown Source) 
     at org.hsqldb.error.Error.error(Unknown Source) 
     at org.hsqldb.rights.UserManager.get(Unknown Source) 
     at org.hsqldb.rights.UserManager.getUser(Unknown Source) 
     at org.hsqldb.Database.connect(Unknown Source) 
     at org.hsqldb.DatabaseManager.newSession(Unknown Source) 
     ... 130 more 

不知它試圖拿起HSQL數據庫作爲數據庫,我不知道爲什麼它不拿起神諭設置。 我還通過以下網址

http://openjpa.apache.org/testing.html

了,並試圖以設置VM參數連接屬性。 請幫忙!!!

+0

我不完全確定如何解決您的問題,但我非常確定您獲取HSQL的原因是因爲那是沒有配置的TomEE回退數據庫。 – Rick

回答

0

我知道它通過以下配置工作。雖然我仍然不知道爲什麼它不適用於openjpa設置。

protected void setUp() throws Exception 
    { 
     final Properties props = new Properties(); 
     props.put("tomee.ejbcontainer.http.port", 1800); 
     props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory"); 
     props.put("ProvAppCLJPA", "new://Resource?type=DataSource"); 
     props.put("ProvAppCLJPA.JdbcUrl", "jdbc:oracle:thin:@localhost:1521:XE"); 
     props.put("ProvAppCLJPA.Password", "XXX"); 
     props.put("ProvAppCLJPA.JdbcDriver", "oracle.jdbc.OracleDriver"); 
     props.put("ProvAppCLJPA.UserName", "PROVAPPCL_DE"); 
     props.put("ProvAppCLJPA.JtaManaged", "true"); 

     ctx = new InitialContext(props); 
    }