private static void _testConnection(
      String driverClassName, String url, String userName, String password) throws Exception {

    Class.forName(driverClassName);

    Connection connection = null;

    try {
      DataSource dataSource =
          DataSourceFactoryUtil.initDataSource(driverClassName, url, userName, password);

      connection = dataSource.getConnection();
    } finally {
      DataAccess.cleanUp(connection);
    }
  }
  @JVMArgsLine("-Dcatalina.base=. -D" + _HIKARICP_JAR_URL + "=${" + _HIKARICP_JAR_URL + "}")
  @NewEnv(type = NewEnv.Type.JVM)
  @Test
  public void testHikariCP() throws Exception {
    RegistryUtil.setRegistry(new BasicRegistryImpl());

    System.setProperty("portal:jdbc.default.liferay.pool.provider", "hikaricp");

    String hikaricpJarURL = System.getProperty(_HIKARICP_JAR_URL);

    if (hikaricpJarURL != null) {
      System.setProperty(
          "portal:" + PropsKeys.SETUP_LIFERAY_POOL_PROVIDER_JAR_URL + "[hikaricp]", hikaricpJarURL);
    }

    InitUtil.init();

    DataSource dataSource = null;

    try (CaptureAppender captureAppender =
        Log4JLoggerTestUtil.configureLog4JLogger(JarUtil.class.getName(), Level.INFO)) {

      dataSource = DataSourceFactoryUtil.initDataSource(_properties);

      List<LoggingEvent> loggingEvents = captureAppender.getLoggingEvents();

      Assert.assertEquals(4, loggingEvents.size());

      LoggingEvent loggingEvent = loggingEvents.get(0);

      String message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Downloading "));

      loggingEvent = loggingEvents.get(1);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Downloaded "));

      loggingEvent = loggingEvents.get(2);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Installing "));

      loggingEvent = loggingEvents.get(3);

      message = (String) loggingEvent.getMessage();

      Assert.assertTrue(message.startsWith("Installed "));
    }

    Class<?> dataSourceClass = dataSource.getClass();

    Assert.assertEquals("com.zaxxer.hikari.HikariDataSource", dataSourceClass.getName());

    for (int i = 0; i < _CHECKOUT_COUNT; i++) {
      Connection connection = dataSource.getConnection();

      PreparedStatement preparedStatement =
          connection.prepareStatement("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");

      preparedStatement.execute();
    }

    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

    ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (TestJDBCPool)");

    int activeConnections = (int) mBeanServer.getAttribute(poolName, "ActiveConnections");

    Assert.assertEquals(_CHECKOUT_COUNT, activeConnections);

    int idleConnections = (int) mBeanServer.getAttribute(poolName, "IdleConnections");

    int totalConnections = (int) mBeanServer.getAttribute(poolName, "TotalConnections");

    Assert.assertEquals(totalConnections, activeConnections + idleConnections);
  }
Esempio n. 3
0
  public static synchronized void init() {
    if (_initialized) {
      return;
    }

    StopWatch stopWatch = new StopWatch();

    stopWatch.start();

    // Set the default locale used by Liferay. This locale is no longer set
    // at the VM level. See LEP-2584.

    String userLanguage = SystemProperties.get("user.language");
    String userCountry = SystemProperties.get("user.country");
    String userVariant = SystemProperties.get("user.variant");

    LocaleUtil.setDefault(userLanguage, userCountry, userVariant);

    // Set the default time zone used by Liferay. This time zone is no
    // longer set at the VM level. See LEP-2584.

    String userTimeZone = SystemProperties.get("user.timezone");

    TimeZoneUtil.setDefault(userTimeZone);

    // Shared class loader

    try {
      PortalClassLoaderUtil.setClassLoader(ClassLoaderUtil.getContextClassLoader());
    } catch (Exception e) {
      e.printStackTrace();
    }

    // Properties

    com.liferay.portal.kernel.util.PropsUtil.setProps(new PropsImpl());

    // Log4J

    if (GetterUtil.getBoolean(SystemProperties.get("log4j.configure.on.startup"), true)) {

      ClassLoader classLoader = InitUtil.class.getClassLoader();

      Log4JUtil.configureLog4J(classLoader);
    }

    // Shared log

    try {
      LogFactoryUtil.setLogFactory(new Log4jLogFactoryImpl());
    } catch (Exception e) {
      e.printStackTrace();
    }

    // Log sanitizer

    SanitizerLogWrapper.init();

    // Java properties

    JavaDetector.isJDK5();

    // Security manager

    SecurityManagerUtil.init();

    if (SecurityManagerUtil.ENABLED) {
      com.liferay.portal.kernel.util.PropsUtil.setProps(
          DoPrivilegedUtil.wrap(com.liferay.portal.kernel.util.PropsUtil.getProps()));

      LogFactoryUtil.setLogFactory(DoPrivilegedUtil.wrap(LogFactoryUtil.getLogFactory()));
    }

    // Cache registry

    CacheRegistryUtil.setCacheRegistry(DoPrivilegedUtil.wrap(new CacheRegistryImpl()));

    // Configuration factory

    ConfigurationFactoryUtil.setConfigurationFactory(
        DoPrivilegedUtil.wrap(new ConfigurationFactoryImpl()));

    // Data source factory

    DataSourceFactoryUtil.setDataSourceFactory(DoPrivilegedUtil.wrap(new DataSourceFactoryImpl()));

    // DB factory

    DBFactoryUtil.setDBFactory(DoPrivilegedUtil.wrap(new DBFactoryImpl()));

    // ROME

    XmlReader.setDefaultEncoding(StringPool.UTF8);

    if (_PRINT_TIME) {
      System.out.println("InitAction takes " + stopWatch.getTime() + " ms");
    }

    _initialized = true;
  }