HarmonyOS rdbstore 在 c++ 层开多线程进行事务操作具体有什么限制?

如题,在 worker 里面 beginTransation 会有异常,执行失败,但用 c 层开启了子线程查询并没有出现异常,测试代码如下

// 创建数据库 config 对象
static napi_value OpenDB(napi_env env, napi_callback_info info)
{
  size_t argc = 1;
  napi_value argv[1] = {nullptr};
  napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);

  // 获取函数argv[1],此为为rawfile相对路径
  size_t strSize;
  char strBuf[256];
  napi_get_value_string_utf8(env, argv[0], strBuf, sizeof(strBuf), &strSize);
  std::string dirName(strBuf, strSize);
  std::cout << "dir is" << dirName << std::endl;


  const char* your_dataBaseDir = dirName.c_str();
  const char* your_storeName = "RdbTest.db";
  const char* your_bundleName = "com.example.ndk";
  const char* your_moduleName = "entry";
  bool your_isEncrypt = false; // 或者true,根据你的需求
  int your_securityLevel = OH_Rdb_SecurityLevel::S1; // 根据你的需求设置
  int your_area = RDB_SECURITY_AREA_EL1; // 根据你的需求设置

  OH_Rdb_Config oh_rdb_config = {
  .selfSize = sizeof(OH_Rdb_Config),
  .dataBaseDir = your_dataBaseDir,
  .storeName = your_storeName,
  .bundleName = your_bundleName,
  .moduleName = your_moduleName,
  .isEncrypt = your_isEncrypt,
  .securityLevel = your_securityLevel,
  .area = your_area
};

  int errCode = 0;
  // 获取获取OH_Rdb_Store实例
  OH_Rdb_Store *store_ = OH_Rdb_GetOrOpen(&oh_rdb_config, &errCode);
  char createTableSql[] = "CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, "
  "AGE INTEGER, SALARY REAL, CODES BLOB)";
  // 执行建表语句
  OH_Rdb_Execute(store_, createTableSql);

  // 创建键值对实例
  OH_VBucket *valueBucket = OH_Rdb_CreateValuesBucket();
  valueBucket->putText(valueBucket, "NAME", "Lisa");
  valueBucket->putInt64(valueBucket, "AGE", 188);
  valueBucket->putReal(valueBucket, "SALARY", 100.5);
  uint8_t arr[] = {1, 2, 3, 4, 5};
  int len = sizeof(arr) / sizeof(arr[0]);
  valueBucket->putBlob(valueBucket, "CODES", arr, len);
  // 插入数据
  int rowId = OH_Rdb_Insert(store_, "EMPLOYEE", valueBucket);
  // 开启线程开启事务查询
  std::thread mythread([=]() {
  int64_t age;
  int transactionId = OH_Rdb_BeginTransaction (store_);
  OH_Predicates *predicates = OH_Rdb_CreatePredicates("EMPLOYEE");
  const char *columnNames[] = {"NAME", "AGE"};
  int len = sizeof(columnNames) / sizeof(columnNames[0]);
  OH_Cursor *cursor = OH_Rdb_Query(store_, predicates, columnNames, len);

  int columnCount = 0;
  cursor->getColumnCount(cursor, &columnCount);

  // OH_Cursor是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始

  while (cursor->goToNextRow(cursor) == OH_Rdb_ErrCode::RDB_OK) {
    cursor->getInt64(cursor, 1, &age);
  }

  OH_Rdb_Commit (store_);

  // 释放谓词实例
  predicates->destroy(predicates);
  // 释放结果集
  cursor->destroy(cursor);
});
  mythread.detach();

  // 销毁键值对实例
  valueBucket->destroy(valueBucket);
  napi_value num;
  napi_create_double(env, rowId, &num);
  return num;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
HarmonyOS
2024-12-25 10:59:33
471浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

目前不能并发操作事务,开启事务和提交事务要在同一个线程中,并且此时另一个线程不能并发的再去开启事务和提交事务。

分享
微博
QQ
微信
回复
2024-12-25 13:14:47
相关问题
HarmonyOS 多线程写法限制
820浏览 • 1回复 待解决
HarmonyOS C++异步操作
1069浏览 • 1回复 待解决
HarmonyOS 数据库多线程操作
954浏览 • 1回复 待解决
HarmonyOS ETS和c++通信
951浏览 • 1回复 待解决
HarmonyOS native C++ 传递buffer 到ArkTS
1122浏览 • 1回复 待解决
ArkTS通过接口访问C++对象
1226浏览 • 1回复 待解决
HarmonyOSC++触发通知到ArkTS
973浏览 • 1回复 待解决
ts给c++传递数组,c++如何解析
2756浏览 • 1回复 待解决
c/c++主动调用ArkTS存在问题
1651浏览 • 1回复 待解决