当应用程序将行插入数据库(postgresql 9.1)时,我的 PlayFramework 2.1.4 应用程序面临问题。事实上,每 20 次插入应用程序都会引发以下异常,导致数据库连接未正确关闭:
c.a.e.c.d.SequenceIdGenerator - Error closing Connection
org.postgresql.util.PSQLException: Cannot change transaction read-only property in the middle of a transaction.
at org.postgresql.jdbc2.AbstractJdbc2Connection.setReadOnly(AbstractJdbc2Connection.java:617) ~[postgresql-9.1-901.jdbc4.jar:na]
at com.jolbox.bonecp.ConnectionHandle.setReadOnly(ConnectionHandle.java:1282) ~[bonecp-0.8.0-rc2-SNAPSHOT.jar:na]
at com.jolbox.bonecp.ConnectionHandle.<init>(ConnectionHandle.java:257) ~[bonecp-0.8.0-rc2-SNAPSHOT.jar:na]
at com.jolbox.bonecp.ConnectionHandle.recreateConnectionHandle(ConnectionHandle.java:276) ~[bonecp-0.8.0-rc2-SNAPSHOT.jar:na]
at com.jolbox.bonecp.ConnectionHandle.close(ConnectionHandle.java:479) ~[bonecp-0.8.0-rc2-SNAPSHOT.jar:na]
at com.avaje.ebean.config.dbplatform.SequenceIdGenerator.closeResources(SequenceIdGenerator.java:244) ~[avaje-ebeanorm-api-3.1.1.jar:na]
这发生在我的开发环境以及使用免费 pgsql 数据库计划的 heroku 开发计划上。
插入到名为“parkingplace”的表中。其中一些是使用 Akka 演员(调度作业)以及使用 POST 查询完成的。结果是一样的:应用程序每插入 20 次就会抛出这个异常。
每次抛出异常时,数据库连接线程都会保持活动状态(关闭连接时出错),并且 Ebean 会创建一个新线程,直到达到线程限制。这导致应用程序在使用每个 DB 线程后无法获取新的 DB 连接(超时异常)。
抛出的异常是:
javax.persistence.PersistenceException: java.sql.SQLException: Timed out waiting for a free available connection. FATAL
at com.avaje.ebeaninternal.server.transaction.TransactionManager.createQueryTransaction(TransactionManager.java:368) ~[avaje-ebeanorm-server-3.1.2.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.createQueryTransaction(DefaultServer.java:2060) ~[avaje-ebeanorm-server-3.1.2.jar:na]
at com.avaje.ebeaninternal.server.core.OrmQueryRequest.initTransIfRequired(OrmQueryRequest.java:181) ~[avaje-ebeanorm-server-3.1.2.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1501) ~[avaje-ebeanorm-server-3.1.2.jar:na]
at com.avaje.ebeaninternal.server.core.DefaultServer.findUnique(DefaultServer.java:1269) ~[avaje-ebeanorm-server-3.1.2.jar:na]
at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findUnique(DefaultOrmQuery.java:922) ~[avaje-ebeanorm-server-3.1.2.jar:na]
Caused by: java.sql.SQLException: Timed out waiting for a free available connection.
当数据库连接超时时,应用程序完全无法访问。
播放的调试日志显示有关错误关闭连接问题的更多详细信息:
2013-10-24 13:10:23,878 - [DEBUG] - from com.avaje.ebean.config.dbplatform.SequenceIdGenerator in Ebean-default.1
... seq:parking_place_seq loaded:20 ids:[1201, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 1217, 1218, 1219, 1220]
似乎 Ebean 为名为 park_place 的表中的下 20 次插入生成了 20 个 ID。当这些 id 被消耗时,Ebean 会生成接下来的 20 个 id,但是在这个特定的 DB 请求期间发生了“错误关闭连接”。
我在 Google/stackoverflow 上没有找到任何关于类似问题的信息,所以我想知道这个问题是与我的应用程序有关还是与 Ebean 的问题有关。
Ebean 版本是 0.8.0-rc2
有没有人知道这个问题的原因?这与Ebean版本有关吗?
非常感谢你的帮助。
编辑 :
这是 play application.conf 文件的 db 属性:
# Database configuration
# ~~~~~
# You can declare as many datasources as you want.
# By convention, the default datasource is named `default`
#
# db.default.driver=org.h2.Driver
# db.default.url="jdbc:h2:mem:play"
# db.default.user=sa
# db.default.password=""
db.default.url="postgres://postgres:xxx@localhost:5432/stras-opendata"
db.default.driver=org.postgresql.Driver
db.default.autocommit=true
db.default.isolation=READ_COMMITTED
db.default.maxConnectionsPerPartition=10
db.default.minConnectionsPerPartition=5
db.default.acquireIncrement=1
db.default.acquireRetryAttempts=2
db.default.acquireRetryDelay=5 seconds
db.default.connectionTimeout=1 second
db.default.idleMaxAge=10 minute
db.default.idleConnectionTestPeriod=5 minutes
db.default.initSQL="SELECT 1"
db.default.logStatements=false
db.default.maxConnectionAge=1 hour
# You can expose this datasource via JNDI if needed (Useful for JPA)
# db.default.jndiName=DefaultDS
这是 20 次插入后“关闭连接错误”所涉及的 Ebean 实体:
@Entity
public class ParkingPlace extends Model implements Parking {
@Id
public Long id;
@ManyToOne
@Column(nullable = false)
@JsonSerialize(using = CheckinerSerializer.class, as = UserAccount.class)
public UserAccount checkiner;
@ManyToOne
@JsonIgnore
public UserAccount checkouter;
@JsonIgnore
public ParkingPlaceStatus status;
@Formats.DateTime(pattern="dd/MM/yyyy HH:mm:ss")
@JsonIgnore
public Date checkoutDate;
@JsonSerialize(using = ParkingPlaceDateSerializer.class)
public Date checkinDate;
public Double latitude;
public Double longitude;
@JsonIgnore
public String comment;
public static Finder<Long,ParkingPlace> find = new Finder<Long,ParkingPlace>(
Long.class, ParkingPlace.class
);
public static List<ParkingPlace> getParkingPlaceOrderByDistance(int limit) {
//String sqlQuery = "select get_distance_metres(48.583478,7.747475,latitude,longitude) as distance from parking_place order by distance limit 20;";
//Ebean.createSqlQuery(sqlQuery).findList();
if(limit==-1) {
return find.where().eq("status",ParkingPlaceStatus.CHECKED_IN).orderBy("get_distance_metres(48.583478,7.747475,latitude,longitude)").findList();
}
return find.where().eq("status",ParkingPlaceStatus.CHECKED_IN).orderBy("get_distance_metres(48.583478,7.747475,latitude,longitude)").setMaxRows(limit).findList();
}
public UserAccount getCheckiner() {
if(checkiner.userName==null) {
return UserAccount.find.byId(checkiner.id);
}
return checkiner;
}
public void checkin(UserAccount user) {
this.checkiner = user;
Date checkinDate = new Date();
this.checkinDate = checkinDate;
this.status = ParkingPlaceStatus.CHECKED_IN;
checkiner.lastCheckin = checkinDate;
checkiner.checkinedPlaces.add(this);
checkiner.save();
}
public void checkout(UserAccount user) {
this.checkouter = user;
Date checkoutDate = new Date();
this.checkoutDate = checkoutDate;
this.status = ParkingPlaceStatus.CHECKED_OUT;
checkouter.checkoutedPlaces.add(this);
checkouter.save();
}
@Override
public Long getId() {
return id;
}
@Override
public Double getLongitude() {
return longitude;
}
@Override
public Double getLatitude() {
return latitude;
}
}