我正在单个服务器上实现乐观事务(BOCC)。在提交时,根据当前数据库状态验证读取和写入集(如果自读取后状态发生更改,则事务将中止)。如果验证成功,所有对象都会写入数据库。
对不同对象集的验证(加上数据库更新)可以是并发的,但必须使用读锁和写锁来保护重叠的对象集。
我使用ReentrantReadWriteLock来确保验证的安全,效果很好。现在我正在编写一个恢复机制,如果由于某些错误(验证后)并非所有对象都写入数据库,该机制会重复更新过程。
因此恢复会重复数据库写入,然后尝试释放锁(恢复成功后)。问题是我尝试从不同的线程释放锁(因为恢复是由另一个后台服务执行的),这会抛出IllegalMonitorStateException. 该方法的注释unlock验证了此行为。
/**
* Attempts to release this lock.
*
* <p>If the current thread is the holder of this lock then
* the hold count is decremented. If the hold count is now
* zero then the lock is released. If the current thread is
* not the holder of this lock then {@link
* IllegalMonitorStateException} is thrown.
*
* @throws IllegalMonitorStateException if the current thread does not
* hold …Run Code Online (Sandbox Code Playgroud) 我有一个 ASP.Net WebAPI 实例设置,它使用 MySQL 数据库进行存储。我编写了一个用于在单个端点请求的生命周期内ActionFilter处理创建 a 的方法。TransactionScope
public async Task<HttpResponseMessage> ExecuteActionFilterAsync(
HttpActionContext actionContext,
CancellationToken cancellationToken,
Func<Task<HttpResponseMessage>> continuation)
{
var transactionScopeOptions = new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted };
using (var transaction = new TransactionScope(TransactionScopeOption.RequiresNew, transactionScopeOptions, TransactionScopeAsyncFlowOption.Enabled))
{
var handledTask = await continuation();
transaction.Complete();
return handledTask;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在整个端点中,我有不同的查询/命令,使用autoenlist=true的功能打开/关闭连接DbConnection。一个示例端点可以是:
public async Task<IHttpActionResult> CreateStuffAsync()
{
var query = this.queryService.RetrieveAsync();
// logic to do stuff
var update = this.updateService.Update(query);
return this.Ok();
}
Run Code Online (Sandbox Code Playgroud)
我不会创建一个DbConnection并从顶部传递它,因为这是一个简单的示例,在实践中传递服务之间的连接需要进行大量重构(尽管如果有必要,可以这样做)。我还读到,最好根据需要打开/关闭连接(即保持它们打开的时间尽可能短)。和 …
在苹果机上:
Traceback (most recent call last):
File "project1/scripts/initializedb.py", line 3, in <module>
import transaction
File "/Users/denmojo/code/project1/env/lib/python3.5/site-packages/transaction/__init__.py", line 19, in <module>
from transaction._transaction import Transaction
File "/Users/denmojocode/project1/env/lib/python3.5/site-packages/transaction/_transaction.py", line 20, in <module>
from zope.interface import implementer
ImportError: No module named 'zope.interface'
Run Code Online (Sandbox Code Playgroud)
无法弄清楚为什么它在那里,但没有导入模块。
Using /Users/denmojo/code/project1/env/lib/python3.5/site-packages/zope.interface-4.3.2-py3.5-macosx-10.6-intel.egg
Run Code Online (Sandbox Code Playgroud) 我的要求是使用 go 实现连接到 redis 集群的管道事务。我正在使用支持 redis 集群、管道和事务的 go-redis 包。如何在不使用 go-redis 包中的 WATCH key 的情况下实现管道事务。我还查看了包中的 Tx.Pipeline() 。在实现事务时是否需要 WATCH key
我们使用 DatabaseTransactions 特征和 MySQL 数据库连接执行单元测试(很多)。
当执行完整的测试套件时,我们得到 15 条左右的“常规错误:1205 超出锁定等待超时;”。当单独执行这些测试时,它们都成功了。
问题主要出现在执行sync()方法时,但不仅限于此。
(尝试增加等待超时,但没有成功)。
任何建议将不胜感激。
也发布在 laracasts 中:https://laracasts.com/discuss/channels/testing/test-suite-general-error-1205-lock-wait-timeout-exceeded
我在 node.js 中有一个处理货币交易的 REST API 端点。我想实现类似于 stripe 的幂等键的东西,以确保在网络故障时同一事务不会执行两次。我如何在 Node.js 中实现类似的东西?
当当前用户点击 likeButton 时,我运行一个事务,但我想检查用户是否已经喜欢该帖子。如果用户已经喜欢该帖子,我想减少 likeCounter 并更改 likeButton 外观,否则我想增加它。
我的 Firestore 数据库使用 collection.document.collection.document... 的东西:
"posts":
- "post1":
- uid: user1
- likeCount: 2
- caption: "caption1"
- "likes":
- "user1":
- value: true
- "user2":
- value: true
- "post2":
- uid: user1
- likeCount: 1
- caption: "caption2"
- "likes":
- "user1":
- value: true
- "user4":
- value: true
- "post3":
- uid: user2
- likeCount: 3
- caption: "caption3"
- "likes":
- "user1":
- value: true
- "user3":
- …Run Code Online (Sandbox Code Playgroud) 我在应用程序中有事务性和普通的生产者,它们正在写入主题 kafka-topic ,如下所示。
事务性 Kafka Producer 的配置
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
// list of host:port pairs used for establishing the initial connections to the Kakfa cluster
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, 5);
/*The amount of time to wait before attempting to retry a failed request to a given topic partition.
* This avoids repeatedly sending requests in a tight loop under some failure scenarios.*/
props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 3);
/*"The configuration controls the …Run Code Online (Sandbox Code Playgroud) transactions apache-kafka kafka-consumer-api spring-kafka kafka-transactions-api
我正在尝试使用 maven 来学习 Spring、Hibernate 和 H2 数据库来构建代码。目前,我遇到一些问题,如何正确使用@Transactional注释来自动启动事务并在entityManager.persist成功完成或回滚时提交事务。
我的测试项目非常简单。POJO 类是 Person,包含名字、姓氏和电子邮件地址。有一个 Service 类 PersonSerice,它是一个提供 CRUD 功能来添加、更改、读取和删除人员数据的接口。有一个 PersonServiceImpl 调用 DAO 类的方法。这里是方法 PersonDAOImpl::createPerson 使用的示例代码
public void createPerson(Person person) {
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
}
Run Code Online (Sandbox Code Playgroud)
一切都按预期进行。有一个 Hibernate SQL 输出
“Hibernate:调用 hibernate_sequence 的下一个值 Hibernate:插入到 person (email, nachname, vorname, id) 值 (?, ?, ?, ?)”
我想摆脱手动调用entityManager.getTransaction().commit(); 所以我尝试在调用DAO方法的ServiceImpl方法处写@Transactional
public void createPerson(Person person) {
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();
}
Run Code Online (Sandbox Code Playgroud)
现在它无法正常工作。我刚刚明白了。“ Hibernate:调用 hibernate_sequence 的下一个值” 有一些内容写入数据库,但如果不手动提交,我无法列出所有条目或删除它们。所以我目前不知道出了什么问题以及如何让 @Transactional 自动执行提交。下面是 Eclipse 调试器中显示的实体管理器内容的一部分:
entityManager $Proxy26 (id=33) h ExtendedEntityManagerCreator$ExtendedEntityManagerInitationHandler (id=116)
containerManaged false
exceptionTranslator null jta …
我尝试执行事务操作并故意抛出异常,以验证回滚是否已完成,但回滚并未执行。
PostgreSQL 数据库版本为 12.1-1,基于 Docker。
这是包含注释的服务@Transactional:
@Service
public class MyTestService {
@Autowired
private DocumentDataDao documentDataDao;
@Transactional
public void test() {
DocumentData data = new DocumentData();
data.setData(UUID.randomUUID().toString());
documentDataDao.create(data);
throw new IllegalArgumentException("Test rollback");
}
}
Run Code Online (Sandbox Code Playgroud)
该create函数使用 aNamedParameterJdbcTemplate插入数据:
String statement = String.format("INSERT INTO %s (%s) VALUES (%s) RETURNING %s", tableName,
String.join(",", insertingColumns), String.join(",", values),
String.join(",", returningColumns));
return getNamedJdbcTemplate().queryForObject(statement, parameters, getRowMapper());
Run Code Online (Sandbox Code Playgroud)
该test函数是从另一个服务调用的:
@Service
public class ApplicationStartupListener {
private Logger log = LoggerFactory.getLogger(ApplicationStartupListener.class);
@Autowired
private MyTestService testService; …Run Code Online (Sandbox Code Playgroud) transactions ×10
java ×3
mysql ×2
apache-kafka ×1
c# ×1
concurrency ×1
firebase ×1
go ×1
hibernate ×1
ios ×1
laravel ×1
locking ×1
node.js ×1
phpunit ×1
pip ×1
pipelining ×1
postgresql ×1
pyramid ×1
python-3.5 ×1
redis ×1
rest ×1
spring ×1
spring-boot ×1
spring-kafka ×1
swift ×1