问题介绍:在weblogic中有时重启项目或启动项目会报错:Unable to resolve 'dataSource'. Resolved ''; remaining name 'dataSource',但是重启weblogic服务可以正常启动项目,项目使用spring框架
解决方案:
- 重启weblogic正常启动项目后查看JNDI树,然后关闭项目观察项目使用的数据源是否从JNDI树中消失,查看JNDI树方法(以12C为例):


- 项目重启时数据源消失是因为,通过代码获取数据源链接时,没有配置对象删除方法,应用关闭时就会删除数据源,可以在@Bean注解中添加destroyMethod 属性,如果是多数据源项目,每个数据源都要设置,如下所示:
@Bean(destroyMethod = "")
public DataSource dataSource() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
DataSource dataSource = dataSourceLookup.getDataSource("dataSource");
return dataSource;
}
如果是xml,配置如下:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="dataSource" />
<property name="destroyMethodName" value="" />
</bean>
当在WebLogic中遇到项目启动报错UnabletoresolvedataSource时,可能是因为应用关闭时未正确配置数据源的删除方法。通过在Spring的@Bean注解中添加destroyMethod属性或在XML配置中设置destroyMethodName,可以确保数据源在应用关闭时不被自动删除,从而避免启动时的问题。重启Weblogic并检查JNDI树可验证此解决方案。

1348

被折叠的 条评论
为什么被折叠?



