コード例 #1
0
  public void convertNewDatabaseSystem() {
    if (existTable("playerstats")) {
      databaseServer.beginTransaction();
      try {
        final SqlUpdate convert =
            databaseServer.createSqlUpdate(
                "INSERT INTO player_stats "
                    + "(playername, kills, deaths, mobkills, killstreak, last_online) "
                    + "SELECT playername, kills, deaths, mobkills, killstreak, ? "
                    + "FROM playerstats");

        // sql parameters begins with one
        convert.setParameter(1, System.currentTimeMillis());
        final int affectedRows = convert.execute();
        final SqlUpdate dropTable = databaseServer.createSqlUpdate("DROP TABLE playerstats");
        dropTable.execute();
        // a drop have to commited and should be rollbackable if process wasn't complete
        databaseServer.commitTransaction();

        Logger.getLogger("ScoreboardStats")
            .log(Level.INFO, "Successfully converted {0} into new table structure", affectedRows);
      } finally {
        databaseServer.endTransaction();
      }
    }
  }
コード例 #2
0
  @Transactional
  public void doSomething(EBasicVer v) {

    EbeanServer server = Ebean.getServer(null);

    inMethodTransaction = server.currentTransaction();

    Transaction t = server.createTransaction();
    SqlUpdate u =
        server.createSqlUpdate("update e_basicver set last_update = last_update+1 where id = ?");
    u.setParameter(1, v.getId());
    int count = server.execute(u, t);
    Assert.assertEquals(1, count);

    t.commit();

    v.setName("some change");
    try {
      Ebean.save(v);
      // never get here
      Assert.assertTrue(false);
    } catch (PersistenceException e) {
      throw e;
    }
  }