该问题我们也折磨了好久:后来才发现解决办法:使用weblogic提供的管理类,可以完成console完成的任何功能,下面提供一端代码:[注意:连接池的配置在7.1后与web与EJB的配置完全不一样,包括加载服务的事件监听]
//动态产生连接池的类
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Set;
import java.util.Properties;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.configuration.JDBCConnectionPoolMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.MBeanHome;
public class DynamicConnectionPoolHelper
{
private Context ctx = null;
private MBeanHome mbeanHome = null;
private ServerMBean serverMBean = null;
// Connection Pool
private JDBCConnectionPoolMBean myConnectionPool = null;
// DataBase attributes
private String dbUsername = "louis ";
private String dbPassword = "louis ";
private String dbURL = "jdbc:weblogic:mssqlserver4 ";
private String dbDriverName = "weblogic.jdbc.mssqlserver4.Driver ";
// Security credentials
private String password = null;
private String serverName = "myserver ";
private String url = null;
private String userName = null;
//产生一个连接池
public JDBCConnectionPoolMBean createConnectionPool(String pNameOfConnectionPool) throws SQLException
{
System.out.println( "Creating Connection Pool... ");
try
{
// Get context
url = "t3://127.0.0.1:7001 ";
userName = "louis ";
password = "louisyyy ";
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(userName);
env.setSecurityCredentials(password);
ctx = env.getInitialContext();
this.mbeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
this.serverMBean = (ServerMBean)mbeanHome.getAdminMBean(serverName, "Server ");
// Get a set of JDBC connection pools for this Domain.
// Test whether the connection pool exists first and if it does, delete it.
Set connectionPools = mbeanHome.getMBeansByType( "JDBCConnectionPool ", mbeanHome.getDomainName());
Iterator iter = connectionPools.iterator();
System.out.println( "Looping through all connection pools... ");
while (iter.hasNext())
{
JDBCConnectionPoolMBean aConnectionPool = (JDBCConnectionPoolMBean)iter.next();
System.out.println(aConnectionPool.getName());
if (aConnectionPool.getName().equals(pNameOfConnectionPool))
{
this.deleteConnectionPool(aConnectionPool);
break;
}
}
// Create Connection Pool
myConnectionPool = (JDBCConnectionPoolMBean)mbeanHome.createAdminMBean(pNameOfConnectionPool, "JDBCConnectionPool ", mbeanHome.getDomainName());
System.out.println( "Creating Connection Pool: " + pNameOfConnectionPool);
Properties props = new Properties();
props.put( "user ", this.dbUsername);
props.put( "password ", this.dbPassword);
props.put( "server ", "127.0.0.1:1034 ");
myConnectionPool.setURL(this.dbURL);
myConnectionPool.setDriverName(this.dbDriverName);
myConnectionPool.setProperties(props);
// Target the Connection Pool to the specified server.
myConnectionPool.addTarget(serverMBean);
System.out.println( "Starting up connection pool. ");
return myConnectionPool;
}
catch (Exception e)
{
throw new SQLException(e.toString());
}
}
//删除一个连接池
public void deleteConnectionPool(JDBCConnectionPoolMBean pConnectionPoolMBean) throws SQLException
{
System.out.println( "Deleting Connection Pool... ");
try
{
// Remove Connection Pool from the server
pConnectionPoolMBean.removeTarget(serverMBean);
// Remove Connection MBean from the configuration
mbeanHome.deleteMBean(pConnectionPoolMBean);
}
catch (Exception ex)
{
throw new SQLException(ex.toString());
}
}
}
//动态产生连接池的类
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Set;
import java.util.Properties;
import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.configuration.JDBCConnectionPoolMBean;
import weblogic.management.configuration.ServerMBean;
import weblogic.management.MBeanHome;
public class DynamicConnectionPoolHelper
{
private Context ctx = null;
private MBeanHome mbeanHome = null;
private ServerMBean serverMBean = null;
// Connection Pool
private JDBCConnectionPoolMBean myConnectionPool = null;
// DataBase attributes
private String dbUsername = "louis ";
private String dbPassword = "louis ";
private String dbURL = "jdbc:weblogic:mssqlserver4 ";
private String dbDriverName = "weblogic.jdbc.mssqlserver4.Driver ";
// Security credentials
private String password = null;
private String serverName = "myserver ";
private String url = null;
private String userName = null;
//产生一个连接池
public JDBCConnectionPoolMBean createConnectionPool(String pNameOfConnectionPool) throws SQLException
{
System.out.println( "Creating Connection Pool... ");
try
{
// Get context
url = "t3://127.0.0.1:7001 ";
userName = "louis ";
password = "louisyyy ";
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(userName);
env.setSecurityCredentials(password);
ctx = env.getInitialContext();
this.mbeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
this.serverMBean = (ServerMBean)mbeanHome.getAdminMBean(serverName, "Server ");
// Get a set of JDBC connection pools for this Domain.
// Test whether the connection pool exists first and if it does, delete it.
Set connectionPools = mbeanHome.getMBeansByType( "JDBCConnectionPool ", mbeanHome.getDomainName());
Iterator iter = connectionPools.iterator();
System.out.println( "Looping through all connection pools... ");
while (iter.hasNext())
{
JDBCConnectionPoolMBean aConnectionPool = (JDBCConnectionPoolMBean)iter.next();
System.out.println(aConnectionPool.getName());
if (aConnectionPool.getName().equals(pNameOfConnectionPool))
{
this.deleteConnectionPool(aConnectionPool);
break;
}
}
// Create Connection Pool
myConnectionPool = (JDBCConnectionPoolMBean)mbeanHome.createAdminMBean(pNameOfConnectionPool, "JDBCConnectionPool ", mbeanHome.getDomainName());
System.out.println( "Creating Connection Pool: " + pNameOfConnectionPool);
Properties props = new Properties();
props.put( "user ", this.dbUsername);
props.put( "password ", this.dbPassword);
props.put( "server ", "127.0.0.1:1034 ");
myConnectionPool.setURL(this.dbURL);
myConnectionPool.setDriverName(this.dbDriverName);
myConnectionPool.setProperties(props);
// Target the Connection Pool to the specified server.
myConnectionPool.addTarget(serverMBean);
System.out.println( "Starting up connection pool. ");
return myConnectionPool;
}
catch (Exception e)
{
throw new SQLException(e.toString());
}
}
//删除一个连接池
public void deleteConnectionPool(JDBCConnectionPoolMBean pConnectionPoolMBean) throws SQLException
{
System.out.println( "Deleting Connection Pool... ");
try
{
// Remove Connection Pool from the server
pConnectionPoolMBean.removeTarget(serverMBean);
// Remove Connection MBean from the configuration
mbeanHome.deleteMBean(pConnectionPoolMBean);
}
catch (Exception ex)
{
throw new SQLException(ex.toString());
}
}
}