【问题标题】:How to connect to DB in hibernate using JNDI如何使用 JNDI 在休眠中连接到数据库
【发布时间】:2014-04-09 09:41:55
【问题描述】:

我正在使用 jndi/db 别名来连接数据库。它工作正常.. 但是,我需要使用 Hibernate 来完成同样的工作......

当我使用以下属性时,我无法连接,

<property name="hibernate.connection.datasource">jdbc/ASPTADMINDEV</property> 

在 resurce.xml 文件中,我正在使用这个,

(
 <factories xmi:type="resources.jdbc:DataSource" xmi:id="DataSource_1233129165895" name="Oracle JDBC Driver DataSource" jndiName="jdbc/ASPTADMINDEV" description="New JDBC Datasource" providerType="Oracle JDBC Driver" authMechanismPreference="BASIC_PASSWORD" authDataAlias="ccixdmgr-nprd1-01-nodeMgr/GITPTDEV-ASPTADMIN-ASPTI" manageCachedHandles="false" logMissingTransactionContext="true" diagnoseConnectionUsage="false" relationalResourceAdapter="builtin_rra" statementCacheSize="10" datasourceHelperClassname="com.ibm.websphere.rsadapter.Oracle10gDataStoreHelper">
      <propertySet xmi:id="J2EEResourcePropertySet_1233129165915">
        <resourceProperties xmi:id="J2EEResourceProperty_1233129165915" name="driverType" type="java.lang.String" value="oci8" description="The type of the driver. The possible values are: thin, oci8." required="false"/>
        <resourceProperties xmi:id="J2EEResourceProperty_1233129165916" name="oracleLogFileSizeLimit" type="java.lang.Integer" value="0" description="Oracle10g and beyond: The oracleLogFileSizeLimit specifies the maximum number of bytes to be written to any one file. Property is relevant only if trace file is specified. Default is unlimited" required="false"/>
        <resourceProperties xmi:id="J2EEResourceProperty_1233129165917" name="oracleLogFileCount" type="java.lang.Integer" value="1" description="Oracle10g and beyond: The oracleLogFileCount specifies the number of files to use. Property is relevant only if trace file is specified. Default is 1." required="false"/>
        <resourceProperties xmi:id="J2EEResourceProperty_1233129165918" name="oracleLogFileName" type="java.lang.String" value="" description="Oracle10g and beyond: The oracleLogFileName indicates which file to write the traces to" required="false"/>
        <resourceProperties xmi:id="J2EEResourceProperty_1233129165919" name="oracleLogTraceLevel" type="java.lang.String" value="INFO" description="Oracle10g and beyond: The oracleLogTraceLevel specifies which message "> 
......... and so

}

请让我知道我在这里做错了什么..

忘记粘贴错误;)..下面是错误,我得到了

INFO: JNDI InitialContext properties:{}
Apr 9, 2014 4:17:19 PM org.hibernate.connection.DatasourceConnectionProvider configure
SEVERE: Could not find datasource: java:comp/env/jdbc/ASPTADMINDEV
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not find datasource
    at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:140) 

.

我需要改变什么吗?我正在使用以下属性

<property name="hibernate.connection.datasource">java:comp/env/jdbc/ASPTADMINDEV</property>

【问题讨论】:

  • 应用部署在哪里:Tomcat还是应用服务器(哪一个)?开始自己调试,例如检查数据源是否正确注册(检查在你的应用服务器中)
  • 哦,你在下面的评论中说你也有评论。有趣...您真的希望我们知道您的系统发生了什么错误吗?
  • @AndreiI 忘记粘贴错误。 ;)。我部署在 TOMCAT 服务器上,它抛出错误

标签: java oracle hibernate jdbc


【解决方案1】:

我认为您需要提供正确的数据源 JNDI 名称,试试这个。

<property name="hibernate.connection.datasource">java:comp/env/jdbc/ASPTADMINDEV</property>

【讨论】:

  • 我试过了,但仍然抛出错误..无法连接到数据库
  • @Arun 请尝试在第一篇文章中添加所有相关细节。验证数据源和您的应用程序是否在相同的上下文/单元/节点等中。我不太喜欢 websphere。您也可以使用连接属性更新帖子。
  • 现在,我已经使用 hibernate fw 创建了简单的 Web 应用程序。我可以直接在休眠配置文件中直接提供瘦 URL 连接到 db。但是,当我尝试通过数据库别名连接时。我正在克服错误。我在resource.xml 中提到了db 别名、jndi 名称。我需要将它包含在 web.xml 中吗?
  • @Arun 是的,web.xml 中的资源引用
猜你喜欢
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
相关资源
最近更新 更多