在Tomcat 6上的JNDI数据源问题,Hibernate

我正在使用Tomcat 6作为应用程序服务器,Struts-Hibernate和MyEclipse 6.0。

我的应用程序使用JDBC驱动程序,但我应该修改它以使用JNDI数据源。 我遵循的步骤如在tomcat 6.0 howto教程中所述。

我在tomcat> conf中定义了我的资源:

<Resource name="jdbc/ats" global="jdbc/ats" auth="Container" type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver" url="jdbc:oracle:thin:@//localhost:1521/MISDEV" username="TEST" password="TEST" maxActive="20" maxIdle="10" maxWait="-1" validationQuery="SELECT 1 from dual" removeAbandoned="true" removeAbandonedTimeout="30" logAbandoned="false"/> 

我在我的应用程序web.xml中提供了参考:

  <resource-ref> <description>Oracle Datasource example</description> <res-ref-name>jdbc/ats</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 

我在我的hibernate-cfg.xml中定义了datasource-dialect

  <property name="connection.datasource">java:comp/env/jdbc/ats</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> 

但是,当我创buildhibernate会话,它不能打开连接:

09:18:11,322错误JDBCExceptionReporter:72 – 无法从底层数据库获取连接! org.hibernate.exception.GenericJDBCException:无法打开连接

我也尝试在运行时设置属性:

  Configuration configuration = new Configuration(); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect"); //configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/ats"); configuration.setProperty("hibernate.current_session_context_class", "thread"); configuration.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider"); configuration.setProperty("hibernate.show_sql", "true"); sessionFactory = configuration.configure().buildSessionFactory(); 

它不会再打开连接。

但是,当我使用JDBC驱动程序它的作品:

 Configuration configuration = new Configuration(); configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect"); //configuration.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/ats"); configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@//localhost:1521/MISDEV"); configuration.setProperty("hibernate.connection.username", "test"); configuration.setProperty("hibernate.connection.password", "test"); configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver"); configuration.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory"); configuration.setProperty("hibernate.current_session_context_class", "thread"); configuration.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider"); configuration.setProperty("hibernate.show_sql", "true"); sessionFactory = configuration.configure().buildSessionFactory(); 

我一直在寻找3天,没有成功。 什么可能是问题?

我也面临同样的问题,但find了一些链接。请看看它。 在2天内,我会尽力做好动手,并让你知道结果。 https://forum.hibernate.org/viewtopic.php?f=1&t=1003866&start=15

他提到了一点

由于tomcat的JNDI是只读的,只有Tomcat本身才有权将对象绑定到JNDI。 因此,对于Hibernate来说,在Context.xml中将数据源声明为Resource是不够的。 您还必须在链接的文章中描述的Context.xml中将UserTransaction声明为Resource,因为Hibernate和JTA期望UserTransaction已经绑定在JNDI上。 也是链接

请尝试一下。