我在GlassFish上有一个Java-JSF Web应用程序,我想在其中使用连接池.因此,我创建了一个applicationscoped bean,它Connection为其他bean的实例提供服务:
public class DatabaseBean {
private DataSource myDataSource;
public DatabaseBean() {
try {
Context ctx = new InitialContext();
ecwinsDataSource = (DataSource) ctx.lookup("jdbc/myDataSource");
} catch (NamingException ex) {
ex.printStackTrace();
}
}
public Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
Connection connection = myDataSource.getConnection();
System.out.println("Succesfully connected: " + connection);
//Sample: Succesfully connected: com.sun.gjc.spi.jdbc40.ConnectionHolder40@7fb213a5
return connection;
}
}
Run Code Online (Sandbox Code Playgroud)
这样连接池的填充速度非常快; 在通过"db-related"视图进行一些导航后,应用程序将停止并显示以下内容:
RAR5117:无法从连接池[mysql_testPool]获取/创建连接.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.RAR5114:分配连接时出错:[分配连接时出错.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.] java.sql.SQLException:分配连接时出错.原因:正在使用的连接等于max-pool-size和expired max-wait-time.无法分配更多连接.
我正在关闭每种方法中的连接和其他资源.应用程序通过独立连接运行一切正常.
我究竟做错了什么?任何提示或建议将不胜感激.
我有一个应用程序,我正在连接到MySQL数据库.它在半夜失去连接,然后喷出null连接,JDBC在X秒内没有收到消息.
getConnection()在我做任何需要与SQL服务器通信的事情之前,我打电话.
这是我的getConnection()方法:
private Connection getConnection() {
try {
if (connection != null) {
if (connection.isClosed() || !connection.isValid(10000)) {
this.initializeRamsesConnection();
}
} else {
this.initializeRamsesConnection();
}
} catch (Exception e) {
debug("Connection failed: " + e);
}
return connection;
}
Run Code Online (Sandbox Code Playgroud)
在initializeRamsesConnection()方法中,我将密码等信息放入字符串中,然后以标准JDBC方式创建连接.
然后我称这个方法:
private Connection getConnectionFromConnectionString() {
Connection con = null;
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);//jdbc sorcery
//if there is no connection string
if (getConnectionString() == null) {
HMIDatabaseAdapter.debug("No connection string");
} …Run Code Online (Sandbox Code Playgroud)