2011-12-05 105 views
1

我已經得到了在JBoss 4.2.3運行並連接到Oracle 10g的數據庫應用的Oracle 10g的連接。我們重構了使用spring roo的應用程序。在此之前試圖配置JNDI,我們使用普通的數據庫連接字符串,它是罰款。但是現在我們試圖使用jndi,並且在JBoss註冊jndi並創建數據源bean時,當hibernate實際上必須打開連接時,它無法打開。Hibernate無法打開使用JNDI JBoss中

這是持久性單元的配置

<persistence-unit name="persistenceUnitDev" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> 
     <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database --> 
     <property name="hibernate.hbm2ddl.auto" value="update"/> 
     <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/> 
     <property name="hibernate.connection.charSet" value="UTF-8"/> 
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.connection.datasource" value="java:jdbc.DataSourceDev"/> 
     <property name="hibernate.validator.apply_to_ddl" value="false" /> 
     <property name="hibernate.validator.autoregister_listeners" value="false" /> 
     <property name="jboss.entity.manager.jndi.name" value="java:jdbc.DataSourceDev"/> 
     <property name="jboss.entity.manager.factory.jndi.name" value="java:/Manager1Factory"/> 
    </properties> 
</persistence-unit> 

的hibernate.cfg.xml

<hibernate-configuration> 
<session-factory> 
    <property name="connection.datasource">java:jdbc.DataSourceDev</property> 
    <property name="connection.autocommit">true</property> 
    <property name="connection.release_mode">after_transaction</property> 
    <property name="show_sql">true</property> 
    <property name="format_sql">true</property> 
    <property name="use_sql_comments">true</property> 
    <!--DB schema will be updated if needed --> 
    <property name="hbm2ddl.auto">update</property> 
</session-factory> 
</hibernate-configuration> 

應用上下文定義的分貝豆:茫然

<jee:jndi-lookup id="jndiDataSource2" jndi-name="${database.jndiName}"/> 


<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="${hibernate.persistenceunit}"/> 
    <property name="dataSource" ref="jndiDataSource2"/> 
</bean> 

我至於爲什麼hibernate似乎無法打開連接。

編輯:這是日誌:

2011-12-05 12:34:51577警告[org.jboss.resource.connectionmanager.JBossManagedConnectionPool]的Throwable在試圖獲得一個新的連接:空 組織。 jboss.resource.JBossResourceException:無法創建連接; - 嵌套的throwable:(java.sql.SQLException:Io exception:網絡適配器無法建立連接) at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:190) at org .jboss.resource.connectionmanager.InternalManagedConnectionPool.createConnectionEventListener(InternalManagedConnectionPool.java:619) 在org.jboss.resource.connectionmanager.InternalManagedConnectionPool.getConnection(InternalManagedConnectionPool.java:264) 在org.jboss.resource.connectionmanager.JBossManagedConnectionPool $ BasePool .getConnection(JBossManagedConnectionPool.java:575) at org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:347) at org.jboss.resource.connectionmanager.TxCon nectionManager.getManagedConnection(TxConnectionManager.java:330) at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:402) at org.jboss.resource.connectionmanager.BaseConnectionManager2 $ ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java: 849)

< - 切 - >

所致:java.sql.SQLException中:IO異常:在oracle.jdbc.driver.DatabaseError.throwSqlException網絡適配器不能建立連接 (DatabaseError .java:112) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)(oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387) at oracle.jdbc.driver.PhysicalConnection。(PhysicalConnection.java)(oracle.jdbc.driver.PhysicalConnection)上的oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255) :441) at oracle.jdbc.driver.T4CConnection。(T4CConnection.java:165) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35) at oracle.jdbc.driver.OracleDriver.connect( OracleDriver.java:801) at org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory。java:172)

+0

我不知道爲什麼你沒有發佈任何異常或日誌痕跡。 – duffymo

+0

檢查您的問題是否與此相同,http://stackoverflow.com/questions/2219417/jdbc-oracle-application-server-and-the-network-adapter-could-not-establish-the – Adi

回答

0

我設法弄清楚了這一點:

下面是我的配置。

<persistence-unit name="persistenceUnitDev" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/> 
     <property name="hibernate.connection.charSet" value="UTF-8"/> 
     <property name="hibernate.connection.datasource" value="dbjndiName"/> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/> 
     <property name="hibernate.connection.autocommit" value="true"/> 
     <property name="hibernate.connection.release_mode" value="after_transaction"/> 
     <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/> 
     <property name="show_sql" value="true"/> 
     <property name="format_sql" value="true"/> 
     <property name="use_sql_comments" value="true"/> 
     <!--DB schema will be updated if needed --> 
     <property name="hbm2ddl.auto" value="create"/> 
     <property name="hibernate.connection.provider_class" value="org.hibernate.connection.DatasourceConnectionProvider"/> 
    </properties> 
</persistence-unit> 

,然後hibernate.cfg.xml中爲空

我的應用程序上下文與以下豆定義:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
</bean> 

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> 

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="persistenceUnitDev"/> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> 
    <property name="environment"> 
     <props> 
      <prop key="java.naming.factory.initial">com.sun.jndi.fscontext.RefFSContextFactory</prop> 
      <prop key="java.naming.provider.url">file:c:/temp</prop> 
     </props> 
    </property> 
</bean> 

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="resourceRef" value="true"/> 
    <property name="jndiName" value="dbjndiName"/> 
    <property name="lookupOnStartup" value="false"/> 
    <property name="cache" value="true"/> 
    <property name="proxyInterface" value="javax.sql.DataSource"/> 
</bean> 

我覺得在這種情況下,它是少了的情況下,以及不使用Java EE JNDI查找並選擇正確的數據源bean類。

相關問題