title: Playframework 2.1/Ebean error doing insert to pgsql : Cannot change transaction read-only property in the middle of a transaction
body: <p>I am facing in issue on my PlayFramework 2.1.4 application when the application is inserting rows to the database (postgresql 9.1). In fact, each 20 inserts the application throws the following exception which caused a DB connection to not properly closed :</p>
  
  <pre><code>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.&lt;init&gt;(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]
  </code></pre>
  
  <p>This is happening on my dev environement as well as on a dev plan on heroku using the free pgsql database plan.</p>
  
  <p>Inserts are done into a table named "parkingplace". Some of them are done using an Akka actor (schedule job) as well as using a POST query. Result is the same : every 20 inserts the application throws this exception.</p>
  
  <p>Each time the exception is thrown a db connection thread is keeping alive (Error closing Connection) and Ebean creates a new thread until the threads limit is reached. This is causing the application to be unable to acquire new DB connection (time out exception) after each DB thread was used.</p>
  
  <p>Exception thrown is:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>When the time out to DB connection is happening the application is completely unreachable.</p>
  
  <p>DEBUG log of play indicates more details about the Error closing connection issue : </p>
  
  <pre><code>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]
  </code></pre>
  
  <p>It seems that Ebean generates 20 ids for the next 20 inserts into the table named parking_place. And when these ids are consumed, Ebean generates the next 20 ids but the "Error closing connection" happened during this particular DB request.</p>
  
  <p>I didn't find anything about a similar issue on Google/stackoverflow, so I wonder if the issue is related to my application or to an issue with Ebean.</p>
  
  <p>Ebean version is 0.8.0-rc2</p>
  
  <p>Does anyone have an idea on the reason of the issue? Is this related to Ebean version?</p>
  
  <p>Thanks a lot for your help.</p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>Here is the db properties of the play application.conf file:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>Here is the Ebean Entity concerned by the "Closing connection error" after 20 inserts :</p>
  
  <pre><code>@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&lt;Long,ParkingPlace&gt; find = new Finder&lt;Long,ParkingPlace&gt;(
              Long.class, ParkingPlace.class
      );
  
      public static List&lt;ParkingPlace&gt; 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;
      }
  }
  </code></pre>