在这里,您可以看到从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;
}
}
答案 0 :(得分:0)
无效连接超时设置为 0 ?