无法使用persistence.xml和hibernate ogm 4.1.1将MongoDB从本地计算机连接到Openshift

时间:2015-02-04 18:15:07

标签: mongodb openshift persistence.xml hibernate-ogm

我正在努力将我在本地计算机上运行的jboss连接到Openshift中的MongoDB数据库。

使用本地数据库(localhost:27017)可以正常工作。

我的persistence.xml:

<?xml version="1.0" encoding="utf-8"?>
<persistence 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_1_0.xsd"
    version="1.0">

    <persistence-unit name="mongodb-PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>

        <class>com.commons.db.model.User</class

        <properties>
            <property name="hibernate.ogm.datastore.provider" value="mongodb" />
            <property name="hibernate.ogm.datastore.database" value="test" />
            <property name="hibernate.ogm.datastore.create_database" value="true" />
            <property name="hibernate.transaction.jta.platform"
                      value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>

            <!-- Running on local machine with local dataabse details -->
            <!-- 
            <property name="hibernate.ogm.mongodb.host" value="localhost" />
            <property name="hibernate.ogm.mongodb.port" value="27017" />
            -->

            <!-- Running on openshift locally with port forwarding -->
            <!-- -->
            <property name="hibernate.ogm.mongodb.host" value="127.0.0.1" />
            <property name="hibernate.ogm.mongodb.port" value="44506" />
            <property name="hibernate.ogm.mongodb.username" value="admin" />
            <property name="hibernate.ogm.mongodb.password" value="password" />
            <!-- -->
        </properties>
    </persistence-unit>
</persistence>

我运行了rch port forwarding命令。

rhc port-forward -a test-app

它返回了正确的端口转发:

haproxy 127.0.0.1:8081   =>  127.4.173.2:8080
haproxy 127.0.0.1:8082   =>  127.4.173.3:8080
java    127.0.0.1:3528   =>  127.4.173.1:3528
java    127.0.0.1:4448   =>  127.4.173.1:4447
java    127.0.0.1:5445   =>  127.4.173.1:5445
java    127.0.0.1:5455   =>  127.4.173.1:5455
java    127.0.0.1:8083   =>  127.4.173.1:8080
java    127.0.0.1:9991   =>  127.4.173.1:9990
java    127.0.0.1:10000  =>  127.4.173.1:9999
mongodb 127.0.0.1:44506  =>  54c685bf5973cac227000040-*****.rhcloud.com:44506
mysql   127.0.0.1:54231  =>  54c687b95973ca28650000a9-*****.rhcloud.com:54231

我可以使用

连接到mongo数据库
mongo --host 127.0.0.1 --port 44506

但是当我尝试使用persistence.xml启动我的jboss服务器时,我向您展示了上面的内容。它总是尝试连接到localhost:27017。返回此错误:

Caused by: org.hibernate.HibernateException: OGM001214: Unable to connect to MongoDB instance localhost:27017
    at org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.extractDatabase(MongoDBDatastoreProvider.java:167)
    at org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.start(MongoDBDatastoreProvider.java:110)
    ... 37 more
Caused by: com.mongodb.MongoTimeoutException: Timed out after 5000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers
=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {j
ava.net.ConnectException: Connection refused}}]
    at com.mongodb.BaseCluster.getDescription(BaseCluster.java:128)
    at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:396)
    at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:569)
    at com.mongodb.DBTCPConnector.getReplicaSetStatus(DBTCPConnector.java:364)
    at com.mongodb.Mongo.getReplicaSetStatus(Mongo.java:465)
    at com.mongodb.DB.getCommandReadPreference(DB.java:86)
    at com.mongodb.DB.command(DB.java:314)
    at com.mongodb.DB.command(DB.java:296)
    at com.mongodb.DB.command(DB.java:371)
    at com.mongodb.Mongo.getDatabaseNames(Mongo.java:396)
    at org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider.extractDatabase(MongoDBDatastoreProvider.java:152)
    ... 38 more

在尝试使用persistence.xml从本地服务器连接到Openshift托管数据库时,是否有任何类似问题?

谢谢

1 个答案:

答案 0 :(得分:3)

您使用的是哪个版本的OGM?

在最新版本中,指定主机和端口的属性为:

hibernate.ogm.datastore.host
hibernate.ogm.datastore.port