【问题标题】:Hibernate can't open a connection with oracle 10g using jndi in JBossHibernate 无法在 JBoss 中使用 jndi 打开与 oracle 10g 的连接
【发布时间】:2011-12-05 10:50:31
【问题描述】:

我有一个在 JBoss 4.2.3 上运行并连接到 Oracle 10g 数据库的应用程序。我们重构了应用程序以使用 spring roo。在尝试配置 jndi 之前,我们使用的是常规的数据库连接字符串,这很好。但是现在我们尝试使用 jndi,当 JBoss 注册 jndi 并创建数据源 bean 时,当 hibernate 实际上必须打开一个连接时,它无法打开。

这是持久化单元的配置

<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>

应用程序上下文定义了 db beans:

<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>

我不知道为什么休眠似乎无法打开连接。

编辑:这是日志:

2011-12-05 12:34:51,577 WARN [org.jboss.resource.connectionmanager.JBossManagedConnectionPool] 尝试获取新连接时可抛出:null org.jboss.resource.JBossResourceException:无法创建连接; - 嵌套的 throwable:(java.sql.SQLException:Io 异常:网络适配器无法建立连接) 在 org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:190) 在 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) 在 org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:347) 在 org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:330) 在 org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:402) 在 org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:849)

原因:java.sql.SQLException:Io异常:网络适配器无法建立连接 在 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) 在 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) 在 oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:255) 在 oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387) 在 oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:441) 在 oracle.jdbc.driver.T4CConnection.(T4CConnection.java:165) 在 oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35) 在 oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801) 在 org.jboss.resource.adapter.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:172)

【问题讨论】:

标签: hibernate jboss connection jndi


【解决方案1】:

我设法弄清楚了:

下面是我的配置。

   <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 定义的:

<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 类的情况。

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2011-03-13
    • 2014-07-11
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多