首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java tomcat应用程序挂起,停止后不能重新启动tomcat

问Java tomcat应用程序挂起,停止后不能重新启动tomcat
EN

Stack Overflow用户
提问于 2019-02-14 09:42:50
回答 1查看 756关注 0票数 0

我已经在tomcat 7上部署了一个成熟的web应用程序到一个新的服务器上。系统使用的数据库是相当空的,因为我们正处于配置它供使用的早期阶段。

进入应用程序,你会得到一个登录页面。登录,它通常会带您到应用程序的主页。

但第二天早上来后,我们总是遇到同样的问题:

  1. 我们打开登录屏幕-没问题
  2. 输入我们的用户名和密码系统挂起。
  3. 我们去tomcat用系统托盘,停止服务。
  4. 然后出现停止服务进度栏,但是tomcat属性对话框上的状态仍然显示“已启动”,并且“开始”和“停止”按钮都被禁用。
  5. 我们检查tomcat日志,没有错误。
  6. 我们重新启动服务器,它再次正常工作。

我们看不出什么明显的东西。tomcat的“查找泄漏”请求没有显示任何信息,查看VisualVM上的堆大小显示了一种一致的堆处理模式,然后是垃圾收集,将其降低到相同的低级别(因此没有明显的泄漏)。

我认为可能是mysql连接超时,但情况不应该是这样,因为如果我使用错误的密码登录,系统会进入数据库检查密码,并按预期返回“错误密码”。它失败的唯一点是输入正确的密码。

我们唯一的线索是,登录时出现了错误,系统使用一些自定义代码来确定用户的主机名:

代码语言:javascript
复制
2019-02-14 08:10:14,277 08:10:14.277 [http-bio-8080-exec-9] ERROR com.sw.app.ui.UserAuthenticatedWebSession - Unknown host!
java.net.UnknownHostException: null
            at java.net.Inet6AddressImpl.getHostByAddr(Native Method) ~[na:1.8.0_201]
            at java.net.InetAddress$2.getHostByAddr(Unknown Source) ~[na:1.8.0_201]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_201]
            at com.sw.app.data.utils.NetUtilities.getHostName(NetUtilities.java:114) ~[app-data-4.0.15.1-SNAPSHOT.jar:na]

只有当用户成功登录以存储他们正在登录的位置时,才会调用这个异常,但是异常在代码中被捕获,然后只是登录而不是向上传播,然后我们使用默认的“未知”主机名。这是代码:

代码语言:javascript
复制
  public static String getHostName( InetAddress inaHost ) throws UnknownHostException
  {
    try {
      Class<? extends InetAddress> clazz = Class.forName( "java.net.InetAddress" ).asSubclass( InetAddress.class );
      Constructor<?>[] constructors = clazz.getDeclaredConstructors();
      constructors[0].setAccessible( true );
      InetAddress ina = (InetAddress)constructors[0].newInstance();
      Field[] fields = ina.getClass().getDeclaredFields();
      for( Field field : fields ) {
        // Depends on the version of java we are dealing with:
        // Older version - single nameservice
        if( field.getName().equals( "nameService" ) ) {
          return getHostName( field.get( null ), inaHost );
        } else if( field.getName().equals( "nameServices" ) ) {
          // newer version - multiple name services possible
          StringBuilder builder = new StringBuilder();
          field.setAccessible( true );
          // A list of nameservice objects
          @SuppressWarnings( "unchecked" )
          List<Object> nameServices = (List<Object>)field.get( null );
          for( Object nameService : nameServices ) {
            String hostName = getHostName( nameService, inaHost );
            if( builder.length() > 0 ) {
              builder.append( ", " );
            }
            builder.append( hostName );
          }
          return builder.toString();
        }
      }
    } catch( ClassNotFoundException cnfe ) {
      throw new InvalidOperationException( "Class not found when looking up host name", cnfe );
    } catch( IllegalAccessException iae ) {
      throw new InvalidOperationException( "Cannot access method/field", iae );
    } catch( InstantiationException ie ) {
      throw new InvalidOperationException( "Cannot instantiate class", ie );
    } catch( InvocationTargetException ite ) {
      throw (UnknownHostException)ite.getCause();
    }
    return null;
  }

  /**
   * Get the host name using reflection on the hidden class implementation of the InetAddress details.
   * @param p_nameService
   * @param p_address
   * @return
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  private static String getHostName( Object nameService, InetAddress address ) throws IllegalAccessException, InvocationTargetException {
    Method[] methods = nameService.getClass().getDeclaredMethods();
    for( Method method : methods ) {
      // The nameService is assumed to have a method, getHostByAddr, which takes the byte[] inet address
      if( method.getName().equals( "getHostByAddr" ) ) {
        method.setAccessible( true );
        return (String)method.invoke( nameService, address.getAddress() );
      }
    }
    return "";
  }

有人有类似的问题吗?

-编辑--

下面是数据库配置bean类。

代码语言:javascript
复制
@Configuration
public class AppPersistence {

  private static final Logger LOGGER = LoggerFactory.getLogger( AppPersistence.class );

  protected static final String INTERNAL_IP_DOMAIN = "*******";
  protected static final String JDBC_PROTOCOL = "jdbc:mysql://";
  protected static final String DEFAULT_DATABASE_NAME = "*******";

  /** The path for context-based property lookups */
  protected static final String CONTEXT_LOOKUP_PATH = "java:comp/env";

  /** This is the default location for the database - on the same machine as the deployment */
  protected static final String DB_LOCAL    = JDBC_PROTOCOL + "localhost:3306/" + DEFAULT_DATABASE_NAME;

  @Bean
  public DataSource createDataSource() throws Exception {
    BasicDataSource source = new BasicDataSource();

    // allow for parameterised config
    source.setDriverClassName( Driver.class.getName() );
    source.setUrl( getProperty( "app.database.url", DB_LOCAL ) );
    source.setUsername( getProperty( "app.database.username", "*****" ) );
    source.setPassword( getProperty( "app.database.password", "****" ) );

    LOGGER.warn( "Connecting to: " + source.getUrl() );

    return source;
  }

  protected String getProperty( String name, String default ) {
    // first check system properties
    String val = System.getProperty( name );
    if( val != null ) {
      logLookup( "System Properties", name, val );
      return val;
    }
    // check environment variables
    val = System.getenv( name );
    if( val != null ) {
      logLookup( "System Environment Variables", name, val );
      return val;
    }
    // if we are deployed to a container, check the environment variables in that.
    try {
      Context context = InitialContext.doLookup( "java:comp/env" );
      if( context != null ) {
        Object valObj = context.lookup( name );
        if( valObj != null ) {
          logLookup( "Context", name, valObj.toString() );
          return valObj.toString();
        }
      }
    } catch( NamingException e ) {
      // if running on a dev machine this will probably happen
      LOGGER.warn( "Could not find context for lookup of " + p_name + " - assuming running in dev mode with defaults.  Error was: " + e.toString( true ) );
      LOGGER.info( "Error received on lookup of " + name + ":", e );
    }
    return p_default;
  }

  protected void logLookup( String source, String lookup, String value ) {
    if( value.contains( "password" ) ) {
      // avoid displaying any password info
      LOGGER.warn( "Successfully looked up sensitive value from " + source + " for name '" + lookup + "': [******]" );
    } else {
      LOGGER.warn( "Successfully looked up value from " + source + " for name '" + lookup + "': '" + value + "'" );
    }
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource ) {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setPersistenceUnitName( "com.sw.app.data.persistence" );
    entityManagerFactory.setDataSource( dataSource );
    entityManagerFactory.setJpaVendorAdapter( new HibernateJpaVendorAdapter() );
    entityManagerFactory.setLoadTimeWeaver( new InstrumentationLoadTimeWeaver() );
    entityManagerFactory.setJpaDialect( new HibernateJpaDialect() );
    entityManagerFactory.setPackagesToScan( "com.sw.app.data", "com.sw.app.rawimport",
                                            "com.sw.app.view", "com.sw.app.warranty" );

    entityManagerFactory.setJpaPropertyMap( hibernateJpaProperties( dataSource ) );
    return entityManagerFactory;
  }

  private Map<String, ?> hibernateJpaProperties( DataSource dataSource ) {
    HashMap<String, String> properties = new HashMap<>();

    // Need to copy these values over, otherwise c3p0 can't see them.
    if( dataSource instanceof BasicDataSource ) {
      BasicDataSource source = (BasicDataSource)p_dataSource;

      properties.put( "hibernate.connection.driver_class", source.getDriverClassName() );
      properties.put( "hibernate.connection.url", source.getUrl() );
      properties.put( "hibernate.connection.username", source.getUsername() );
      properties.put( "hibernate.connection.password", source.getPassword() );
    }

    // Added to avoid some merge problems when updating entities (eg contact to custimport)
    properties.put( "hibernate.event.merge.entity_copy_observer", "allow" );

    // Second level cache
    properties.put( "hibernate.cache.use_second_level_cache", "true" );
    properties.put( "hibernate.cache.use_query_cache", "true" );
    properties.put( "hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider" );
    properties.put( "hibernate.cache.region.factory_class", EhCacheRegionFactory.class.getName() );
    properties.put( "hibernate.generate_statistics", "false" );

    properties.put( "hibernate.show_sql", "false" );
    properties.put( "hibernate.format_sql", "false" );

    // validate | update | create | create-drop -->
    properties.put( "hibernate.hbm2ddl.auto", "update" );
    properties.put( "hibernate.dialect", MySQL5Dialect.class.getName() );

    // [main] WARN  org.hibernate.cfg.AnnotationBinder - HHH000457: Joined inheritance hierarchy [com.sw.system4.data.collateral.AbstractCollateral] defined explicit @DiscriminatorColumn.  Legacy Hibernate behavior was to ignore the @DiscriminatorColumn.  However, as part of issue HHH-6911 we now apply the explicit @DiscriminatorColumn.  If you would prefer the legacy behavior, enable the `hibernate.discriminator.ignore_explicit_for_joined` setting (hibernate.discriminator.ignore_explicit_for_joined=true) -->
    properties.put( "hibernate.discriminator.ignore_explicit_for_joined", "true" );

    //properties.put("hibernate.hbm2ddl.import_files", "insert-data.sql");
    //properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");

    // This wasnt required in persistence.xml, but for some reason is here.
    properties.put( "hibernate.connection.provider_class", C3P0ConnectionProvider.class.getName() );

    // just adding c3p0 props was enough in persistence.xml, but not here.
    properties.put( "hibernate.c3p0.min_size", "5" );
    properties.put( "hibernate.c3p0.max_size", "20" );
    properties.put( "hibernate.c3p0.timeout", "300" ); // 5mins
    properties.put( "hibernate.c3p0.max_statements", "50" );
    properties.put( "hibernate.c3p0.idle_test_period", "100" );
    properties.put( "hibernate.c3p0.preferredTestQuery", "select 1" );
    properties.put( "hibernate.c3p0.testConnectionOnCheckout", "true" );
    properties.put( "hibernate.c3p0.numHelperThreads", "12" );
    properties.put( "hibernate.c3p0.maxStatementsPerConnection", "25" );
    properties.put( "hibernate.c3p0.statementCacheNumDeferredCloseThreads", "1" );

    return l_properties;
  }

  @Bean
  public JpaTransactionManager transactionManager( EntityManagerFactory emf ) {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
    jpaTransactionManager.setEntityManagerFactory( emf );
    return jpaTransactionManager;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2019-02-15 09:39:37

你的数据库是什么?是云数据库吗?我对CloudSQL也有类似的问题。发生的情况是,当一些活动连接没有对数据库做任何事情时,从数据库端来说,几个小时后它就会拒绝连接。但是在应用程序端,您将把它看作是一个活动连接。我使用了Apache池,并且能够在数据库配置中使用这个问题。

代码语言:javascript
复制
    dataSource.setValidationQuery("SELECT 1");
    dataSource.setTestOnBorrow(true);
    dataSource.setTestWhileIdle(true);

由于您使用的是C3P0,下面的命令应该适用于您。

代码语言:javascript
复制
    hibernate.dbcp.testOnBorrow=true
    hibernate.dbcp.testOnReturn=true
    hibernate.dbcp.validationQuery=SELECT 1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54687331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档