我正在尝试使用Java DataSource
连接到我的数据库。我正在使用Weblogic 12c和Hibernate来实现这一目标。我的web.xml
文件包含以下资源定义:
<resource-ref>
<res-ref-name>jdbc/Microtek</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
此外,我的weblogic.xml
包含以下内容:
<wls:resource-description>
<wls:res-ref-name>jdbc/Microtek</wls:res-ref-name>
<wls:jndi-name>microtek</wls:jndi-name>
</wls:resource-description>
在我的Hibernate配置文件(hibernate.cfg.xml
)中,我设置了以下属性:
<property name="hibernate.connection.datasource">jdbc/Microtek</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="javax.persistence.validation.mode">none</property>
此外,在META-INF
目录中,我创建了一个名为context.xml
的XML文件,其中包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<ResourceLink global="jdbc/Microtek" name="jdbc/Microtek" type="javax.sql.DataSource"/>
</Context>
最后,这是我得到的例外:
org.hibernate.HibernateException: Could not find datasource
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:79)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
at com.microtekcomputers.models.test.main(test.java:15)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
... 8 more
这是我第一次使用JNDI连接到数据源,所以我可能会遗漏一些东西。所有的想法和建议非常感谢。
更新:在进行其他人建议的更改后,我现在遇到以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/kernel/KernelStatus
at weblogic.jndi.Environment.<clinit>(Environment.java:81)
at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at org.hibernate.util.NamingHelper.getInitialContext(NamingHelper.java:51)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:143)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:84)
at org.hibernate.cfg.SettingsFactory.createConnectionProvider(SettingsFactory.java:459)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:91)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2831)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2827)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
at com.microtekcomputers.models.Test.main(Test.java:12)
答案 0 :(得分:1)
您没有为数据源定义连接工厂,如代码所示:
environment.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
或者在hibernate.cfg.xml中:
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
在weblogic中,您可以根据需要配置连接工厂,WLInitialContextFactory是默认设置。