我在与Maven的JSF项目中使用JPA(EclipseLink)。在运行时期间,抛出以下异常:No suitable driver found for jdbc:postgresql://localhost:5432/project
之后必须重新启动GlassFish Server。
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: No suitable driver found for jdbc:postgresql://localhost:5432/project?loginTimeout=0&socketTimeout=0&prepareThreshold=5&unknownLength=2147483647&loglevel=0&tcpkeepalive=false&binaryTransfer=true
at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:103)
at com.sun.enterprise.resource.pool.ConnectionPool.addResource(ConnectionPool.java:282)
at com.sun.enterprise.resource.pool.ConnectionPool.createResourceAndAddToPool(ConnectionPool.java:1512)
at com.sun.enterprise.resource.pool.ConnectionPool.createResources(ConnectionPool.java:944)
at com.sun.enterprise.resource.pool.ConnectionPool.resizePoolAndGetNewResource(ConnectionPool.java:792)
at com.sun.enterprise.resource.pool.ConnectionPool.getResourceFromPool(ConnectionPool.java:760)
at com.sun.enterprise.resource.pool.ConnectionPool.getUnenlistedResource(ConnectionPool.java:632)
at com.sun.enterprise.resource.pool.ConnectionPool.internalGetResource(ConnectionPool.java:526)
at com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:381)
at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:360)
at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
... 53 more
我已经尝试了一切。我找了好几个小时来解决这个问题。我的假设是连接池配置不正确或者事务Scoped(容器管理)EntityManager没有正确关闭连接。
以下是该项目的一些代码片段:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="projectPU" transaction-type="JTA">
<jta-data-source>jdbc/project_customer_pool</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
的glassfish-resources.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-resource enabled="true"
jndi-name="jdbc/project_customer_pool"
object-type="user"
pool-name="project_customer_pool">
<description>Project Customer Pool</description>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false"
associate-with-thread="false"
connection-creation-retry-attempts="0"
connection-creation-retry-interval-in-seconds="10"
connection-leak-reclaim="true"
connection-leak-timeout-in-seconds="15"
statement-timeout-in-seconds="8"
statement-leak-timeout-in-seconds="4"
connection-validation-method="auto-commit"
datasource-classname="org.postgresql.ds.PGConnectionPoolDataSource"
fail-all-connections="false"
idle-timeout-in-seconds="300"
is-connection-validation-required="false"
is-isolation-level-guaranteed="true"
lazy-connection-association="false"
lazy-connection-enlistment="false"
match-connections="false"
max-connection-usage-count="0"
max-pool-size="32"
max-wait-time-in-millis="60000"
name="project_customer_pool"
non-transactional-connections="false"
pool-resize-quantity="2"
res-type="javax.sql.ConnectionPoolDataSource"
steady-pool-size="8"
validate-atmost-once-period-in-seconds="0"
wrap-jdbc-objects="false">
<description>project db</description>
<property name="serverName" value="localhost"/>
<property name="portNumber" value="5432"/>
<property name="databaseName" value="project"/>
<property name="URL" value="jdbc:postgresql://localhost:5432/project"/>
<property name="User" value="admin"/>
<property name="Password" value="admin"/>
</jdbc-connection-pool>
</resources>
数据库访问对象(DAO):
@Stateless
public class DAO implements IDAO, Serializable {
@PersistenceContext(unitName = "projectPU")
private EntityManager em;
// Example for a method
@Override
public Benutzer login(String email) {
if (email.isEmpty()) {
return null;
}
try {
TypedQuery<User> qu = em.createQuery("FROM User u WHERE u.email = :email AND u.valid_to IS null", User.class);
qu.setParameter("email", email);
return qu.getSingleResult();
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
return null;
}
}
我希望有人可以帮我解决这个问题,或者知道如何解决这个问题。我不知道该怎么办了。
谢谢!