我做了一个非常简单的Hiernate的程序,用普通的jdbc是没问题的,但是如果用tomcat的JNDI数据源就有问题了,出现错误:
xception in thread "main " org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:56)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:124)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:56)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:366)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:60)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
at hinernate.HiberOperator. <init> (HiberOperator.java:21)
at hinernate.Test.main(Test.java:14)
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: [Root exception is java.lang.ClassNotFoundException: ]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext. <init> (Unknown Source)
at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:28)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:52)
... 8 more
Caused by: java.lang.ClassNotFoundException:
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 14 more
但是我的数据源是存在并且可用的,我的hibernat.cfg.xml是:
<?xml version= '1.0 ' encoding= 'UTF-8 '?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name= "connection.datasource "> java:comp/env/jdbc/MyJndi </property>
<property name= "connection.username "> root </property>
<property name= "connection.password "> root </property>
<property name= "dialect "> org.hibernate.dialect.MySQLDialect </property>
<property name= "jndi.url "> jdbc:mysql://localhsot:3306/FishDB </property>
<property name= "jndi.class "> </property>
<mapping resource= "bean/Stockinfo.hbm.xml " />
</session-factory>
</hibernate-configuration>
请问高手是什么原因呢?谢谢
------解决方案--------------------