我正在开发一个Web项目,最近我安装了postgres 9.1.1
postgresql服务器已启动并正在运行.我可以像往常一样通过psql连接,一切都被加载并从我从8.5制作的db的转储中正确保存.
所以我也在这里下载了9.1 postgres版本的JDBC4驱动程序:http://jdbc.postgresql.org/download/postgresql-jdbc-9.1-901.src.tar.gz
我通过eclipse使用项目属性将它添加到java构建路径中.
这是我用来提供与其他类的数据库连接的代码(即它是一个单例,只有当现有关闭或空时,我才能获得一个新连接,一次只能从一个对象获得)
public abstract class DBConnection {
private static Connection connection = null;
public static void connect() {
try {
if (connection == null) {
String host = "127.0.0.1";
String database = "xxxxx";
String username = "xxxxx";
String password = "xxxxx";
String url = "jdbc:postgresql://" + host + "/" + database;
String driverJDBC = "org.postgresql.Driver";
Class.forName(driverJDBC);
connection = DriverManager.getConnection(url, username,
password); //line firing the class not found exception
} else if (connection.isClosed()) { …Run Code Online (Sandbox Code Playgroud) 我在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.无法分配更多连接.
我正在关闭每种方法中的连接和其他资源.应用程序通过独立连接运行一切正常.
我究竟做错了什么?任何提示或建议将不胜感激.