java - 为什么在weblogic中LeakedConnectionCount总是0?

标签 java weblogic12c

在这里,您可以看到从 weblogic 获取泄漏连接计数的程序。但是,我总是把它当作零。我已将数据源中的最大连接计数设置为 10,并且运行了一些不会关闭连接的代码,因此,连接不可用计数增加到 10,但喜欢的连接计数仍为 0。

那么,如何增加它的数量,因为应用程序中有喜欢的连接。

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Test  {

private static final Logger logger = Logger.getLogger(Test.class.getName());
private static final String PROTOCOL = "admin.server.protocol";
private static final String HOST = "admin.server.host";
private static final String PORT = "admin.server.port";
private static final String USERNAME = "admin.server.username";
private static final String PASSWORD = "admin.server.password";
private static final String JNDI_ROOT = "jndi.root";
private static final String DATA_SOURCE = "DataSourceName";
private static final String DATASOURCE_ORACLEDS_JTA = "dataSource-OracleDS_jta";
private static MBeanServerConnection connection;
private static JMXConnector connector;
public static void main(String[] args) throws InterruptedException {
    Test test = new Test();
    System.out.println(test.isConnectionLeaked());

}

public List<String> getDataSourceNames(){
    return Arrays.asList(DATA_SOURCE,DATASOURCE_ORACLEDS_JTA);
}


/*
 * Initialize connection to the Domain Runtime MBean Server.
 */
 public static void initConnection() throws IOException,
        MalformedURLException {
    logger.info("Inside initConnection");
    InputStream is = ClassLoader.getSystemResourceAsStream("jmx.properties");
    Properties props = new Properties();
    props.load(is);

    String protocol = (String) props.get(PROTOCOL);
    Integer portInteger = Integer.valueOf((String) props.get(PORT));
    int port = portInteger.intValue();
    String jndiroot = (String) props.get(JNDI_ROOT);
    String hostname = (String) props.get(HOST);
    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot);

    Hashtable<String, Object> h = new Hashtable<>();
    h.put(Context.SECURITY_PRINCIPAL, (String) props.get(USERNAME));
    h.put(Context.SECURITY_CREDENTIALS, (String) props.get(PASSWORD));
    h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
    h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
    connector = JMXConnectorFactory.connect(serviceURL, h);
    connection = connector.getMBeanServerConnection();
    logger.info("End initConnection");
}
public boolean isConnectionLeaked() {
    List<String> dataPoolNames = getDataSourceNames();
    boolean isLeaked = false;
    try {
        initConnection();

        ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,"
                + "Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
        ObjectName[] number_of_servers = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
        int length = (int) number_of_servers.length;
        for (int i = 0; i < length; i++) {
            logger.info("Server Instance=" + number_of_servers[i]);
            String name = (String) connection.getAttribute(number_of_servers[i], "Name");
            ObjectName[] number_of_dbpools = (ObjectName[]) connection.getAttribute(new ObjectName("com.bea:Name="
                    + name + ",ServerRuntime=" + name + ",Location=" + name + ",Type=JDBCServiceRuntime"),
                    "JDBCDataSourceRuntimeMBeans");
            int pool_length = (int) number_of_dbpools.length;
            for (int x = 0; x < pool_length; x++) {
                String poolName = (String) connection.getAttribute(number_of_dbpools[x], "Name");

                    logger.info("********* PoolName=" + poolName + "  ******");

                    int leakedConnectionCount = (Integer) connection.getAttribute(number_of_dbpools[x],
                            "LeakedConnectionCount");
                    logger.info("leakedConnectionCount         : " + leakedConnectionCount);

                    if (leakedConnectionCount > 0) { // Send email alert
                        isLeaked = true;
                    }

            }
        }
    } catch (Exception e) {
        logger.severe("Exception in isConnectionLeaked method");
        logger.severe("Message = " + e.getMessage());
    } finally {
        try {
            if (connector != null) {
                logger.info("Connectors  JMXConnector.");
                connector.close();
            }
        } catch (IOException e) {
            logger.severe(e.getMessage());
        }
    }
    logger.info("End isConnectionLeaked. isLeaked = "+isLeaked);
    return isLeaked;
}

}

最佳答案

非 Activity 连接超时设置为0

关于java - 为什么在weblogic中LeakedConnectionCount总是0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553381/

相关文章:

Java字符串数组按照3个标准排序

java - Oracle 是用 C 还是 Java 创建的?

java - 为什么不使用这种更简单的 Java 图形实现?

java - 从 Weblogic 9 调用部署在 Weblogic 12 中的远程 EJB 时出错

java - jersey rest 应用程序未在 weblogic 12.1.3 中使用共享库 jax-rs 2.0 进行部署

spring - WebLogic 12.2.1 : Override Jackson library in common_modules with version in EAR

java - 在数据库中搜索文本(使用 Hibernate Search)

java - 不同环境下的 Spring Properties 文件配置和 Maven 集成

oracle - Maven:将 Weblogic 库放入本地存储库时出错

java - 验证 weblogic 中的 JDBC 驱动程序是否已正确更新?