我正在研究一个发布DDL的程序.我想知道是否CREATE TABLE可以回滚和类似的DDL
描述每个数据库如何使用DDL处理事务.
我对事务与锁定表有点混淆,以确保数据库完整性,并确保SELECT和UPDATE保持同步,没有其他连接干扰它.我需要:
SELECT * FROM table WHERE (...) LIMIT 1
if (condition passes) {
// Update row I got from the select
UPDATE table SET column = "value" WHERE (...)
... other logic (including INSERT some data) ...
}
Run Code Online (Sandbox Code Playgroud)
我需要确保没有其他查询会干扰并执行相同的操作SELECT(在连接完成更新行之前读取'旧值'.
我知道我可以默认LOCK TABLES table只确保一次只有一个连接正在执行此操作,并在完成后解锁它,但这看起来有点矫枉过正.在事务中包装它会做同样的事情(确保没有其他连接尝试相同的进程而另一个仍处理)?或者会更好SELECT ... FOR UPDATE还是SELECT ... LOCK IN SHARE MODE更好?
假设我有一个查询:
begin tran
-- some other sql code
Run Code Online (Sandbox Code Playgroud)
然后我忘了提交或回滚.
如果另一个客户端尝试执行查询,会发生什么?
我想在多个表上运行多个insert语句.我正在使用dapper.net.我没有看到任何方法来处理与dapper.net的交易.
请分享您对如何使用dapper.net进行交易的想法.
为什么我在Hibernate中需要Transaction才能进行只读操作?
以下事务是否锁定了数据库?
从DB获取的示例代码:
Transaction tx = HibernateUtil.getCurrentSession().beginTransaction(); // why begin transaction?
//readonly operation here
tx.commit() // why tx.commit? I don't want to write anything
Run Code Online (Sandbox Code Playgroud)
我可以用session.close() 而不是tx.commit()吗?
我读过大约4级隔离:
Isolation Level Dirty Read Nonrepeatable Read Phantom Read
READ UNCOMMITTED Permitted Permitted Permitted
READ COMMITTED -- Permitted Permitted
REPEATABLE READ -- -- Permitted
SERIALIZABLE -- -- --
Run Code Online (Sandbox Code Playgroud)
我想了解每个事务隔离对表的锁定
READ UNCOMMITTED - no lock on table
READ COMMITTED - lock on committed data
REPEATABLE READ - lock on block of sql(which is selected by using select query)
SERIALIZABLE - lock on full table(on which Select query is fired)
Run Code Online (Sandbox Code Playgroud)
下面是事务隔离中可能发生的三种现象
Dirty Read - no lock
Nonrepeatable Read - 没有脏读作为锁定提交数据
Phantom …
我正在编写一个集成测试,我将把一些对象插入数据库,然后检查以确定我的方法是否检索这些对象.
我与数据库的连接是通过NHibernate ...而我创建这样一个测试的常用方法是执行以下操作:
NHibernateSession.BeginTransaction();
//use nhibernate to insert objects into database
//retrieve objects via my method
//verify actual objects returned are the same as those inserted
NHibernateSession.RollbackTransaction();
Run Code Online (Sandbox Code Playgroud)
但是,我最近发现了TransactionScope,它显然可以用于这个目的......
public static int AddDepartmentWithEmployees(Department dept)
{
int res = 0;
DepartmentAdapter deptAdapter = new DepartmentAdapter();
EmployeeAdapter empAdapter = new EmployeeAdapter();
using (TransactionScope txScope = new TransactionScope())
{
res += deptAdapter.Insert(dept.DepartmentName);
//Custom method made to return Department ID
//after inserting the department "Identity Column"
dept.DepartmentID = deptAdapter.GetInsertReturnValue(); …Run Code Online (Sandbox Code Playgroud) 有人可以提供一个简单(但不简单)的交易解释,应用于计算(即使从维基百科复制)?
有谁知道在SQL Server 2000数据库中列出打开事务的任何方法?
我知道我可以查询sys.dm_tran_session_transactionsSQL 2005(及更高版本)数据库版本的视图,但这在SQL 2000上不可用.
我有一个读取查询,我在一个事务中执行,以便我可以指定隔离级别.查询完成后,我该怎么办?
做每一个有什么含义?
using (IDbConnection connection = ConnectionFactory.CreateConnection())
{
using (IDbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
{
using (IDbCommand command = connection.CreateCommand())
{
command.Transaction = transaction;
command.CommandText = "SELECT * FROM SomeTable";
using (IDataReader reader = command.ExecuteReader())
{
// Read the results
}
}
// To commit, or not to commit?
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:问题不在于是否应该使用交易或是否有其他方法来设置交易级别.问题是,是否提交或回滚了不修改任何内容的事务.有性能差异吗?它会影响其他连接吗?还有其他差异吗?
transactions ×10
sql ×4
database ×3
c# ×2
java ×2
sql-server ×2
.net ×1
commit ×1
concurrency ×1
create-table ×1
dapper ×1
ddl ×1
failover ×1
hibernate ×1
locking ×1
mysql ×1
nhibernate ×1
theory ×1