本文整理了Java中com.j256.ormlite.dao.Dao.startThreadConnection()
方法的一些代码示例,展示了Dao.startThreadConnection()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Dao.startThreadConnection()
方法的具体详情如下:
包路径:com.j256.ormlite.dao.Dao
类名称:Dao
方法名:startThreadConnection
[英]WARNING: This method is for advanced users only. It is only to support the #setAutoCommit(DatabaseConnection,boolean) and other methods below. Chances are you should be using the #callBatchTasks(Callable) instead of this method unless you know what you are doing.
This allocates a connection for this specific thread that will be used in all other DAO operations. The thread must call #endThreadConnection(DatabaseConnection) once it is done with the connection. It is highly recommended that a try { conn = dao.startThreadConnection(); ... } finally { dao.endThreadConnection(conn); }
type of pattern be used here to ensure you do not leak connections.
[中]警告:此方法仅适用于高级用户。它仅支持#setAutoCommit(DatabaseConnection,boolean)和下面的其他方法。除非您知道自己在做什么,否则您应该使用#callBatchTasks(Callable)而不是此方法。
这将为此特定线程分配一个连接,该线程将在所有其他DAO操作中使用。一旦连接完成,线程必须调用#endThreadConnection(DatabaseConnection)。强烈建议在此处使用try { conn = dao.startThreadConnection(); ... } finally { dao.endThreadConnection(conn); }
类型的模式,以确保连接不会泄漏。
代码示例来源:origin: j256/ormlite-core
/**
* @see Dao#startThreadConnection()
*/
@Override
public DatabaseConnection startThreadConnection() {
try {
return dao.startThreadConnection();
} catch (SQLException e) {
logMessage(e, "startThreadConnection() threw exception");
throw new RuntimeException(e);
}
}
代码示例来源:origin: com.j256.ormlite/ormlite-core
/**
* @see Dao#startThreadConnection()
*/
@Override
public DatabaseConnection startThreadConnection() {
try {
return dao.startThreadConnection();
} catch (SQLException e) {
logMessage(e, "startThreadConnection() threw exception");
throw new RuntimeException(e);
}
}
代码示例来源:origin: ikidou/Retrofit2Demo
List<Blog> blogList = generateDefaultBlogList();
Dao<Blog, Long> blogDao = DaoManager.createDao(source, Blog.class);
DatabaseConnection databaseConnection = blogDao.startThreadConnection();
boolean autoCommit = databaseConnection.isAutoCommit();
databaseConnection.setAutoCommit(false);
代码示例来源:origin: com.j256.ormlite/ormlite-jdbc
@Override
public void run() {
try {
DatabaseConnection conn2 = dao.startThreadConnection();
try {
assertNotNull(dao.queryForId(foo.id));
} finally {
if (conn2 != null) {
dao.endThreadConnection(conn2);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
代码示例来源:origin: j256/ormlite-core
@Test
public void testCallBatchTasksCommitted() throws Exception {
final Dao<Foo, Integer> dao = createDao(Foo.class, true);
final Foo foo1 = new Foo();
DatabaseConnection conn = dao.startThreadConnection();
try {
dao.callBatchTasks(new Callable<Void>() {
@Override
public Void call() throws Exception {
assertEquals(1, dao.create(foo1));
assertNotNull(dao.queryForId(foo1.id));
return null;
}
});
dao.rollBack(conn);
assertNotNull(dao.queryForId(foo1.id));
} finally {
dao.endThreadConnection(conn);
}
}
代码示例来源: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
DatabaseConnection conn1 = null;
try {
conn1 = dao.startThreadConnection();
assertTrue(dao.isAutoCommit(conn1));
dao.setAutoCommit(conn1, false);
代码示例来源: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);
}
}
}
代码示例来源:origin: com.j256.ormlite/ormlite-jdbc
DatabaseConnection conn = null;
try {
conn = dao.startThreadConnection();
assertTrue(dao.isAutoCommit(conn));
dao.setAutoCommit(conn, false);
内容来源于网络,如有侵权,请联系作者删除!