关闭Spring应用程序会使数据源的JNDI名称远离jdbc上下文 [英] Shutting down Spring application makes JNDI name for datasource go away from jdbc context

查看:178
本文介绍了关闭Spring应用程序会使数据源的JNDI名称远离jdbc上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Weblogic
服务器上使用Spring MVC和Spring Data JPA设置Web应用程序。第一次将它部署到Weblogic服务器时应用程序正常工作但是当我停止应用程序时,我的数据源的jndi名称(jdbc / myDS)从我的Weblogic服务器上的JNDI树中消失,然后当我尝试启动应用程序时我再次收到以下错误:

I'm trying to setup a web application using Spring MVC and Spring Data JPA on my Weblogic server. The application works fine the first time I deploy it to the Weblogic server but when I stop the application the jndi name (jdbc/myDS) to my datasource disappears from the JNDI Tree on my Weblogic server, and then when I try to start the application again I get the following error:

Caused By: javax.naming.NameNotFoundException: Unable to resolve 'jdbc.myDS'. Resolved 'jdbc'; remaining name 'myDS'

我在启动时在JPAConfiguratation.java中设置了以下内容:

I'm setting up the following at startup in JPAConfiguratation.java:

package mySpringApp.application;

import java.util.Properties;

import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * An application context Java configuration class. The usage of Java configuration
 * requires Spring Framework 3.0 or higher with following exceptions:
 * <ul>
 *     <li>@EnableWebMvc annotation requires Spring Framework 3.1</li>
 * </ul>
 */
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
@ImportResource("classpath:applicationContext.xml")
@PropertySource("classpath:application.properties")
public class JPAConfiguration{

    private static final Logger logger = LoggerFactory.getLogger(JPAConfiguration.class);

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
    private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";

    @Resource
    private Environment environment;

    @Bean
    public DataSource dataSource() throws NamingException {    
        Context ctx = new InitialContext();
        String jndiName = "jdbc/myDS";
        DataSource dataSourceJNDINAME = (DataSource) ctx.lookup(jndiName);
        return dataSourceJNDINAME;
    ));


    @Bean
    public JpaTransactionManager transactionManager() throws ClassNotFoundException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();

        transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());

        return transactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() throws ClassNotFoundException {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

        try {
            entityManagerFactoryBean.setDataSource(dataSource());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("Error setting datasource for entityManagerFactoryBean", e);
            logger.error(e.getMessage());
        }
        entityManagerFactoryBean.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);

        Properties jpaProterties = new Properties();
        jpaProterties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        jpaProterties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
        jpaProterties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
        jpaProterties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));

        entityManagerFactoryBean.setJpaProperties(jpaProterties);

        return entityManagerFactoryBean;
    }

Web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">

    <servlet>
        <servlet-name>MySpringApp</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>mySpringApp.application</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MySpringApp</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

第一次关闭应用程序时的日志输出:

The log output when shutting down the application the first time:

INFO  - onfigWebApplicationContext - Closing WebApplicationContext for namespace 'MySpringApp-servlet': startup date [Thu Oct 03 13:13:05 CEST 2013]; root of context hierarchy
DEBUG - DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
INFO  - DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c51f5cb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcesso
r,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.sprin
gframework.context.annotation.internalPersistenceAnnotationProcessor,webConfig,JPAConfiguration,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,homeController,firstController,buildController,
greetingController,repositoryBuildService,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.data.repository.core.sup
port.RepositoryInterfaceAwareBeanPostProcessor#1,org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration,requestMappingHandlerMapping,mvcContentNegotiationManager,viewControllerHandlerMapping,beanNameHandlerMapp
ing,resourceHandlerMapping,defaultServletHandlerMapping,requestMappingHandlerAdapter,mvcConversionService,mvcValidator,httpRequestHandlerAdapter,simpleControllerHandlerAdapter,handlerExceptionResolver,messageSource,viewResolver,org.spr
ingframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.Http
RequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#1,buil
dRepository,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#2,org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration,org.springframework.transaction.config.internal
TransactionAdvisor,transactionAttributeSource,transactionInterceptor,dataSource,transactionManager,entityManagerFactoryBean,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#1,org.springframework.web.servlet.handler.S
impleUrlHandlerMapping#2,org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#1,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#3,org.springframework.data.repository.core.support.RepositoryInterface
AwareBeanPostProcessor#3]; root of factory hierarchy
DEBUG - DisposableBeanAdapter      - Invoking destroy() on bean with name 'org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration'
DEBUG - DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)': [(inner bean), buildRepository]
DEBUG - DisposableBeanAdapter      - Invoking destroy() on bean with name 'entityManagerFactoryBean'
INFO  - erEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'mySpringAppPersistenceUnit'
DEBUG - SessionFactoryImpl         - HHH000031: Closing
DEBUG - tityManagerFactoryRegistry - Remove: name=mySpringAppPersistenceUnit
DEBUG - DisposableBeanAdapter      - Invoking destroy method 'shutdown' on bean with name 'dataSource'
DEBUG - DisposableBeanAdapter      - Invoking destroy() on bean with name 'JPAConfiguration'
DEBUG - DisposableBeanAdapter      - Invoking destroy() on bean with name 'webConfig'
DEBUG - DisposableBeanAdapter      - Invoking destroy() on bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration'

我正在使用:


  • 春天3.2.4.RELEASE

  • Hibernate 4.2.6.Final

  • Weblogic 10.3.5

我需要以某种方式手动处理应用程序的关闭?是什么导致jndi名称从服务器上下文中消失?

Do I need to handle the shutdown of the application manually somehow? What can cause the jndi name disappear from the server context?

非常感谢所有帮助!

推荐答案

我遇到了同样的问题。添加destroyMethod =为我修复了它。

I had the same problem. Adding destroyMethod="" fixed it for me.

显然,如果没有destroyMethod,Spring会尝试确定destroy方法是什么。这显然导致关闭数据源并从树中删除JNDI密钥。将其更改为会强制它不要查找destroyMethod。

Apparently if there is no destroyMethod, Spring tries to determine what the destroy method is. This is apparently causing the datasource to be closed and the JNDI key to be removed from the tree. Changing it to "" forces it to not look for a destroyMethod.

@Bean(destroyMethod = "")
public DataSource dataSource() throws NamingException{
    Context context = new InitialContext();
    return (DataSource)context.lookup("jdbc.mydatasource");
}

这篇关于关闭Spring应用程序会使数据源的JNDI名称远离jdbc上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆