com.j256.ormlite.dao.Dao.setAutoCommit()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(144)

本文整理了Java中com.j256.ormlite.dao.Dao.setAutoCommit()方法的一些代码示例,展示了Dao.setAutoCommit()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.setAutoCommit()方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:setAutoCommit

Dao.setAutoCommit介绍

[英]Set auto-commit mode to be true or false on the connection returned by the #startThreadConnection(). This may not be supported by all database types.

WARNING: Chances are you should be using the #callBatchTasks(Callable) instead of this method unless you know what you are doing.

NOTE: Depending on your underlying database implementation and whether or not you are working with a single database connection, you may need to synchronize calls to here and calls to #callBatchTasks(Callable), #commit(DatabaseConnection), and #rollBack(DatabaseConnection).
[中]在#startThreadConnection()返回的连接上,将自动提交模式设置为true或false。这可能不是所有数据库类型都支持的。
警告:除非您知道自己在做什么,否则您可能应该使用#callBatchTasks(Callable)而不是此方法。
注意:根据您的基础数据库实现以及您是否使用单个数据库连接,您可能需要同步对此处的调用以及对#callBatchTasks(可调用)、#commit(DatabaseConnection)和#rollBack(DatabaseConnection)的调用。

代码示例

代码示例来源:origin: com.j256.ormlite/ormlite-core

/**
 * @see Dao#setAutoCommit(DatabaseConnection, boolean)
 */
@Override
public void setAutoCommit(DatabaseConnection connection, boolean autoCommit) {
  try {
    dao.setAutoCommit(connection, autoCommit);
  } catch (SQLException e) {
    logMessage(e, "setAutoCommit(" + connection + "," + autoCommit + ") threw exception");
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: j256/ormlite-core

/**
 * @see Dao#setAutoCommit(DatabaseConnection, boolean)
 */
@Override
public void setAutoCommit(DatabaseConnection connection, boolean autoCommit) {
  try {
    dao.setAutoCommit(connection, autoCommit);
  } catch (SQLException e) {
    logMessage(e, "setAutoCommit(" + connection + "," + autoCommit + ") threw exception");
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

conn1 = dao.startThreadConnection();
assertTrue(dao.isAutoCommit(conn1));
dao.setAutoCommit(conn1, false);
assertFalse(dao.isAutoCommit(conn1));
  dao.setAutoCommit(conn1, true);
  dao.endThreadConnection(conn1);

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testAutoCommitClose() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  DatabaseConnection conn = null;
  try {
    conn = dao.startThreadConnection();
    dao.setAutoCommit(conn, false);
    Foo foo = new Foo();
    assertEquals(1, dao.create(foo));
    List<Foo> results = dao.queryForAll();
    assertEquals(1, results.size());
    try {
      closeConnectionSource();
    } catch (SQLException e) {
      // ignore any exceptions (stupid derby)
    }
    conn = null;
    DaoManager.clearCache();
    openConnectionSource();
    dao = createDao(Foo.class, true);
    results = dao.queryForAll();
    // we expect there to be no results because we closed the connection to the database before a commit
    // happened
    assertEquals(0, results.size());
  } finally {
    dao.endThreadConnection(conn);
  }
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testAutoCommitClose() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  DatabaseConnection conn = null;
  try {
    conn = dao.startThreadConnection();
    dao.setAutoCommit(conn, false);
    Foo foo = new Foo();
    assertEquals(1, dao.create(foo));
    List<Foo> results = dao.queryForAll();
    assertEquals(1, results.size());
    dao.endThreadConnection(conn);
    conn = null;
    after();
    DaoManager.clearCache();
    before();
    dao = createDao(Foo.class, true);
    results = dao.queryForAll();
    // we expect there to be no results because we closed the connection to the database before a commit
    // happened
    assertEquals(0, results.size());
  } finally {
    if (conn != null) {
      dao.endThreadConnection(conn);
    }
  }
}

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

conn = dao.startThreadConnection();
assertTrue(dao.isAutoCommit(conn));
dao.setAutoCommit(conn, false);
assertFalse(dao.isAutoCommit(conn));
  dao.setAutoCommit(conn, true);
  dao.endThreadConnection(conn);

代码示例来源:origin: com.j256.ormlite/ormlite-jdbc

@Test
public void testConnectionRollback() throws Exception {
  JdbcPooledConnectionSource pooled = new JdbcPooledConnectionSource(DEFAULT_DATABASE_URL);
  Dao<Foo, Integer> dao = null;
  DatabaseConnection conn = null;
  try {
    TableUtils.createTable(pooled, Foo.class);
    dao = DaoManager.createDao(pooled, Foo.class);
    conn = dao.startThreadConnection();
    dao.setAutoCommit(conn, false);
    Foo foo = new Foo();
    assertEquals(1, dao.create(foo));
    assertNotNull(dao.queryForId(foo.id));
    dao.endThreadConnection(conn);
    assertNull(dao.queryForId(foo.id));
  } finally {
    TableUtils.dropTable(pooled, Foo.class, true);
    if (dao != null) {
      dao.endThreadConnection(conn);
    }
    pooled.close();
  }
}

代码示例来源:origin: j256/ormlite-core

@Test
public void testConnectionMethods() throws Exception {
  Dao<Foo, Integer> dao = createDao(Foo.class, true);
  DatabaseConnection conn = null;
  try {
    conn = dao.startThreadConnection();
    assertTrue(dao.isAutoCommit(conn));
    dao.setAutoCommit(conn, false);
    assertFalse(dao.isAutoCommit(conn));
    Foo foo = new Foo();
    assertEquals(1, dao.create(foo));
    assertNotNull(dao.queryForId(foo.id));
    dao.rollBack(conn);
    assertNull(dao.queryForId(foo.id));
    foo = new Foo();
    assertEquals(1, dao.create(foo));
    assertNotNull(dao.queryForId(foo.id));
    dao.commit(conn);
    assertNotNull(dao.queryForId(foo.id));
    dao.rollBack(conn);
    assertNotNull(dao.queryForId(foo.id));
  } finally {
    if (conn != null) {
      conn.setAutoCommit(true);
      dao.endThreadConnection(conn);
    }
  }
}

相关文章