Can multiple threads write to the database?

I am a little confused from the statement in the documentation: "Berkeley DB Data Store does not support locking, and hence does not guarantee correct behavior if more than one thread of control is updating the database at a time."
1. Can multiple threads write to the "Simple Data Store"?
2. Considering the sample code below which writes to the DB using 5 threads - is there a possibility of data loss?
3. If the code will cause data loss, will adding DB_INIT_LOCK and/or DB_INIT_TXN in DBENV->open make any difference?
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <db.h>
static DB *db = NULL;
static DB_ENV *dbEnv = NULL;
DWORD WINAPI th_write(LPVOID lpParam)
DBT key, data;
char key_buff[32], data_buff[32];
DWORD i;
printf("thread(%s) - start\n", lpParam);
for (i = 0; i < 200; ++i)
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
sprintf(key_buff, "K:%s", lpParam);
sprintf(data_buff, "D:%s:%8d", lpParam, i);
key.data = key_buff;
key.size = strlen(key_buff);
data.data = data_buff;
data.size = strlen(data_buff);
db->put(db, NULL, &key, &data, 0);
Sleep(5);
printf("thread(%s) - End\n", lpParam);
return 0;
int main()
db_env_create(&dbEnv, 0);
dbEnv->open(dbEnv, NULL, DB_CREATE | DB_INIT_MPOOL | DB_THREAD, 0);
db_create(&db, dbEnv, 0);
db->open(db, NULL, "test.db", NULL, DB_BTREE, DB_CREATE, 0);
CreateThread(NULL, 0, th_write, "A", 0, 0);
CreateThread(NULL, 0, th_write, "B", 0, 0);
CreateThread(NULL, 0, th_write, "B", 0, 0);
CreateThread(NULL, 0, th_write, "C", 0, 0);
th_write("C");
Sleep(2000);
}

Here some clarification about BDB Lock and Multi threads behavior
Question 1. Can multiple threads write to the "Simple Data Store"?
Answer 1.
Please Refer to http://docs.oracle.com/cd/E17076_02/html/programmer_reference/intro_products.html
A Data Store (DS) set up
(so not using an environment or using one, but without any of the DB_INIT_LOCK, DB_INIT_TXN, DB_INIT_LOG environment regions related flags specified
each corresponding to the appropriate subsystem, locking, transaction, logging)
will not guard against data corruption due to accessing the same database page and overwriting the same records, corrupting the internal structure of the database etc.
(note that in the case of the Btree, Hash and Recno access methods we lock at the database page level, only for the Queue access method we lock at record level)
So,
if You want to have multiple threads in the application writing concurrently or in parallel to the same database You need to use locking (and properly handle any potential deadlocks),
otherwise You risk corrupting the data itself or the database (its internal structure).
Of course , If You serialize at the application level the access to the database, so that no more one threads writes to the database at a time, there will be no need for locking.
But obviously this is likely not the behavior You want.
Hence, You need to use either a CDS (Concurrent Data Store) or TDS (Transactional Data Store) set up.
See the table comparing the various set ups, here: http://docs.oracle.com/cd/E17076_02/html/programmer_reference/intro_products.html
Berkeley DB Data Store
The Berkeley DB Data Store product is an embeddable, high-performance data store. This product supports multiple concurrent threads of control, including multiple processes and multiple threads of control within a process. However, Berkeley DB Data Store does not support locking, and hence does not guarantee correct behavior if more than one thread of control is updating the database at a time. The Berkeley DB Data Store is intended for use in read-only applications or applications which can guarantee no more than one thread of control updates the database at a time.
Berkeley DB Concurrent Data Store
The Berkeley DB Concurrent Data Store product adds multiple-reader, single writer capabilities to the Berkeley DB Data Store product. This product provides built-in concurrency and locking feature. Berkeley DB Concurrent Data Store is intended for applications that need support for concurrent updates to a database that is largely used for reading.
Berkeley DB Transactional Data Store
The Berkeley DB Transactional Data Store product adds support for transactions and database recovery. Berkeley DB Transactional Data Store is intended for applications that require industrial-strength database services, including excellent performance under high-concurrency workloads of read and write operations, the ability to commit or roll back multiple changes to the database at a single instant, and the guarantee that in the event of a catastrophic system or hardware failure, all committed database changes are preserved.
So, clearly DS is not a solution for this case, where multiple threads need to write simultaneously to the database.
CDS (Concurrent Data Store) provides locking features, but only for multiple-reader/single-writer scenarios. You use CDS when you specify the DB_INIT_CDB flag when opening the BDB environment: http://docs.oracle.com/cd/E17076_02/html/api_reference/C/envopen.html#envopen_DB_INIT_CDB
TDS (Transactional Data Store) provides locking features, adds complete ACID support for transactions and offers recoverability guarantees. You use TDS when you specify the DB_INIT_TXN and DB_INIT_LOG flags when opening the environment. To have locking support, you would need to also specify the DB_INIT_LOCK flag.
Now, since the requirement is to have multiple writers (multi-threaded writes to the database),
then TDS would be the way to go (CDS is useful only in single-writer scenarios, when there are no needs for recoverability).
To Summarize
The best way to have an understanding of what set up is needed, it is to answer the following questions:
- What is the data access scenario? Is it multiple writer threads? Will the writers access the database simultaneously?
- Are recoverability/data durability, atomicity of operations and data isolation important for the application? http://docs.oracle.com/cd/E17076_02/html/programmer_reference/transapp_why.html
If the answers are yes, then TDS should be used, and the environment should be opened like this:
dbEnv->open(dbEnv, ENV_HOME, DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_INIT_LOG | DB_RECOVER | DB_THREAD, 0);
(where ENV_HOME is the filesystem directory where the BDB environment will be created)
Question 2. Considering the sample code below which writes to the DB using 5 threads - is there a possibility of data loss?
Answer 2.
Definitely yes, You can see data loss and/or data corruption.
You can check the behavior of your testcase in the following way
1. Run your testcase
2.After the program exits
run db_verify to verify the database (db_verify -o test.db).
You will likely see db_verify complaining, unless the thread scheduler on Windows weirdly starts each thread one after the other,
IOW no two or ore threads write to the database at the same time -- kind of serializing the writes
Question 3. If the code will cause data loss, will adding DB_INIT_LOCK and/or DB_INIT_TXN in DBENV->open make any difference?
Answer 3.
In Your case the TDS should be used, and the environment should be opened like this:
dbEnv->open(dbEnv, ENV_HOME, DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | DB_INIT_LOG | DB_RECOVER | DB_THREAD, 0);
(where ENV_HOME is the filesystem directory where the BDB environment will be created)
doing this You have proper deadlock handling in place and proper transaction usage
so
You are protected against potential data corruption/data loss.
see http://docs.oracle.com/cd/E17076_02/html/gsg_txn/C/BerkeleyDB-Core-C-Txn.pdf
Multi-threaded and Multi-process Applications
DB is designed to support multi-threaded and multi-process applications, but their usage
means you must pay careful attention to issues of concurrency. Transactions help your
application's concurrency by providing various levels of isolation for your threads of control. In
addition, DB provides mechanisms that allow you to detect and respond to deadlocks.
Isolation means that database modifications made by one transaction will not normally be
seen by readers from another transaction until the first commits its changes. Different threads
use different transaction handles, so this mechanism is normally used to provide isolation
between database operations performed by different threads.
Note that DB supports different isolation levels. For example, you can configure your
application to see uncommitted reads, which means that one transaction can see data that
has been modified but not yet committed by another transaction. Doing this might mean
your transaction reads data "dirtied" by another transaction, but which subsequently might
change before that other transaction commits its changes. On the other hand, lowering your
isolation requirements means that your application can experience improved throughput due
to reduced lock contention.
For more information on concurrency, on managing isolation levels, and on deadlock
detection, see Concurrency (page 32).

Similar Messages

  • Multiple Threads writing to the same stream and "Write End Dead"

    Hi, I'm wondering what is the proper method for having multiple threads write to the same stream.
    Right now I have PipedWriter that I give to several Threads.
    However the JLS specifies that when the thread writing to PipedWriter dies, an exception will be generated. Does it mean that I may get a "write end dead" exception when any of the threads dies?
    More specifically I'm trying to extract keywords from a set of pages and print out keywords with their contexts (using threads to avoid a single blocked pages from blocking entire process)

    Your topic says "stream" but your post says "writer" - there's a difference. Readers/writers use the native encoding (which is correct if, for example, you're reading .DOC files created in Windows) while streams use UTF-8 encoding (which is correct if you've got one java app talking to another).
    Multiple threads can output to the same source. You've probably seen really annoying examples as your own System.out.println()s get intermixed with other JVM output in the JVM's window. Synchronization is going to be a serious issue.
    As to the exception, why not just catch it and ignore it?

  • While syncing it says that it can't read/write to the iPod. I've reset to factory settings and it still won't work.

    Every time I connect it to sync it says it's been sync'd with another library and do I want to erase and sync w/ the current itune library.....it has never been sync'd with another itunes library....just this one on my mac.  When it starts to sync and says that it can't read/write to the ipod. I've restarted it, reset it and made sure my itune is the most recent.  HELP.

    When you still have the problem after restoring to factory defaults/new iPod then you very likely have a hardware problem and an appointment a th Genius Bar of an Apple store is in order.

  • I can not stop or start the Database logged with sidadm

    Hello !!
    I migrated ECC6 SAP / MaxDB 7.6.00.35 Windows 2003 Server to Windows 2008 Server. But now when I perform a login with sidadm, can not stop or start the database in dbmcli.
    Just with Administrator can stop or start the database.
    Error After  db_offline :
    Err -24994, ERR_RTE: Runtime enviroment error 1, could not create comm. Shared memory [5]
    sdbverify ok :
    SAP Utilities 7.6.00.35 64 bit in d:/sapdb/
    check files... ok
    check dependencies... ok
    package data is consistent
    DB Analyzer 7.6.00.35 64 bit in d:/sapdb/pr
    check files... ok
    check dependencies... ok
    package data is consistent
    db_enum
    OK
    P01     D:\sapdb\P01\db                         7.6.00.35       fast    running
    P01     D:\sapdb\P01\db                         7.6.00.35       quick   offline
    P01     D:\sapdb\P01\db                         7.6.00.35       slow    offline
    P01     D:\sapdb\P01\db                         7.6.00.35       test    offline
    Anyone have any ideas or know of any problem with windows 2008?
    tks

    Hello,
    -> Please check, if the solution of  SAP Note No. 688028 will help you with reported issue
    -> I also recommend to update the MAXDB, the version is quite old and there many fixes done in the new release patches of 7.6.
    Regards, Natalia Khlopina

  • Can multiple threads share the same cursor in berkeley db java edition?

    We use berkeley db to store our path computation results. We now have two threads which need to retrieve records from database. Specifically, the first thread accesses the database from the very beginning and read a certain number of records. Then, the second thread needs to access the database and read the rest records starting from the position where the cursor stops in the first thread. But, now, I cannot let these two threads share the same cursor. So, I have to open the database separately in two threads and use individual cursor for each thread. This means I have to in the second thread let the cursor skip the first certain number of records and then read the rest records. However, in this way, it is a waste of time letting the second thread skip a certain of records. It will be ideal for us that the second thread can start reading the record just from the place where the first thread stops. Actually, I have tried using transactional cursor and wanted to let the two threads share the same transactional cursor. But it seems that this didn't work.
    Can anyone give any suggestion? Thank you so much!
    sgao

    If your question is really about using the BDB Java Edition product please post to the JE forum:
    Berkeley DB Java Edition
    If your question is about using the Java API of the BDB (C-based) product, then this is the correct forum.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Can Multiple users work on the same work Repository ?

    I have master repository and work repository on one machine, can multiple developers connect andwork on the same work repository? how?

    oh Yes!
    it is v simple.
    follow the steps:-
    once master and work repository has been created on a system.U just need to know all the information supplied wen creating a login to designer.
    like user name and password of database,url,driver as well as master's repository uname and password.
    if u have the following information with you,then u can create a new login with designer provided all the above information and u will have full access to designer
    in work repository u want to connect

  • Can multiple people post on the same shared photo stream?

    Is there a way to have multiple people post on the same shared photo stream?
    My cousin would like to have a single shared photo stream where we can both post photos on it and comment, instead of having two separate shared photo streams. 
    We think that there should be an option for the album creator to give access to anyone he/she shares it with to enable them to also post pics on the same stream.

    The only link I have is to manage your team account http://forums.adobe.com/thread/1460939?tstart=0
    The only Adobe program/process for team work that I know of is Adobe Anywhere, but that requires a very different process
    Adobe Anywhere http://www.adobe.com/products/adobeanywhere.html
    http://www.creativeimpatience.com/adobe-anywhere-enterprise-solution/

  • Write to the database from a BI Publisher report

    I have a requirement to setup a BI Publisher report that writes a snapshot of data to a database table whenever the report is run.
    Putting aside whether doing this is a good idea or not (I'd prefer not to do it for security reasons) if I were to do it in SQL server (that i'm more familiar with) I'd write a stored procedure that does this and enter "exec storedprocedurename" as the report query.
    From searching through this board I see that I'm supposed to use a pipelined function instead of a stored procedure but I'm unclear if once I have setup the pipelined function that I'll be able to achieve my objective of both returning the data to the report as well as writing it to a snapshot table, is this possible? If so then I have some questions as I don't really understand the syntax with my limited pl/sql knowledge.
    create type numset_t as table of number; --where does this go, it doesn't appear to be part of the function?
    create function f1(x number) return numset_t pipelined is
    begin
    for i in 1..x loop
    pipe row(i);
    end loop; -- do I need to do a looP? i just want to do select *...
    return;
    end;
    select * from table(f1(3)); -- again this appears to be outside the function, where does it go?
    Thanks
    Bruce

    Jorge:
    I was looking into something similar some time ago. The suggested solution may or may not work for you, depending on what it is you want to write to the tables. I was trying to write audit information - who ran what report when. Through BI Publisher template variables I could get the who and the when, but not what report was being run, so the report trigger option wasn't of any use to me.
    In the end, I opted to change the attributes of the report so that the "show controls" checkbox is unchecked and make users run all reports through BI Publisher Scheduler. It turns out that reports run via the scheduler have all of that data saved off and you can see it from the Schedules/History display.
    Good luck,
    Gaff

  • How can multiple Mac users have the same iTunes?

    I have a MacBook, and my wife and I have separate accounts on it. It's easier that way for separate Facebook accounts, email, etc. However, it's complicated when it comes to iTunes and applications. We both want to use the same iTunes library. We have iTunes Match, but I'd rather not re-download my entire library when it's already on my computer just so we both have access to it. I tried following the directions at this article, but it didn't work. So, how can both my wife and I, from different accounts on the same Mac (and using the same Apple ID) access the same iTunes library and have any changes reflected on both accounts?

    Obviously, if you just need to copy those iTunes folders, you don't need the ability to write; just the ability to read, which you already have.
    Besides, the /Users/Shared folder is indeed an excellent location for a shared iTunes library; I hadn't yet considered that. Ignore all the above instructions. First, make sure your two iTunes libraries are in sync. Use an external hard drive (or mess with permissions) to do what copying you need to. Then go into iTunes and iTunes>Preferences...>Advanced and change the iTunes Media folder location to /Users/Shared. All of your iTunes content should be copied (or moved) into /Users/Shared.
    After that, do the same thing in the other account, and both your iTunes libraries will now be pointed to the same location.

  • What can I tune to make the database use less memory in a 1GB ram notebook?

    Hi,
    Our notebook has 1GB ram only, running Windows XP Pro, Oracle10g Database, Oracle9i Application Server. It consumes about 1.4 - 1.5GB ram where it's already start using the virtual memory, therefore have some performance issue with it especially when try to startup the services.
    I'm not a DBA but I would like to get some advice on what and how can I tune the database to use less memory since the notebook is used for demo application only, so we save memory resource and make the notebook runs faster.
    Please advise.
    Thank you.

    Application Server 10gR1, 10gR2 is certified against WinXP:
    XP      10g (9.0.4.0.1)      N/A      Desupported       None       Yes      N/A
    XP      10g (9.0.4)      N/A      Desupported      None      Yes      N/A
    XP      10g (10.1.3.1.0)      N/A      Certified      Yes      None      None
    XP SP2+      10g (10.1.3.0.0)      N/A      Certified      None      Yes      Yes
    XP SP2      10g (10.1.2.0.2)      N/A      Certified      None      Yes      Yes
    XP SP2      10g (10.1.2.0.1)      N/A      Certified      None      Yes      Yes
    XP Xeon      10g (10.1.2.0.0)      N/A      Certified      None      Yes      Yes
    XP      10g (10.1.2.0.0)      N/A      Certified      None      Yes      Yes
    XP      9.0.3      N/A      Desupported      None      Yes      N/A
    XP      9.0.2.0.1      N/A      Desupported      None      None      N/ADessupported means Oracle will no longer produce patches or provide maintenance for the version, so in case you hit some new bug the only alternative will be to work with a higher version.
    In your case you could use 10g on XP. The only XP not supported is Home Edition.
    Coming back to the issue, since 9iR2 Oracle recommendation is to provide 1G for each AS node (infrastructure and middle tier). If you have this on the same box, by itself on the Enterprise install will consume more than 1G.
    Tuning 10g database won't make much difference. If you install AS 10gR2 it includes a 10gR1 Database as infrastructure database you could use for demo purposes.
    ~ Madrid.

  • Can multiple APEX application use the same parsing schema?

    Hi,
    I have 4.2 APEX thru pl/sql Gatewat, 11gr2 DB and using theme 24.
    Due to the APEX limitation for version control I would be splitting 1 big ERP applications into 24 different APEX applications and each application would be considered as 1 unit for version control.
    I have about 800 tables and I would assume that all of these would need to be stored in 1 schema since a lot of these table are linked thru FK.
    Can I have multiple APEX APPS using the same parsing schema? or is there a better way to do this?
    Thanks in advance!

    Hi,
    Multiple applications can have same (one) parsing schema.
    You can test that on e.g. apex.oracle.com, where normally you have only one schema and you create multiple applications and all use that same schema.
    Regards,
    Jari
    My Blog: http://dbswh.webhop.net/htmldb/f?p=BLOG:HOME:0
    Twitter: http://www.twitter.com/jariolai
    Edited by: jarola on Jan 28, 2013 7:15 PM

  • Database write using the Database Adapter

    i'm using Jdeveloper version 11.1.1.3.37.55.99 - and I'm not sure that I'm doing this right but:
    I've created a database table that has 3 attributes (id, name, description).
    I've then in the soa composite created a Database Adapter to have an insert operation for the newly created database table.
    In the BPM editor - I drag a service task into the process and configure it to use the recently created Database Adapter.
    While configuring the service task - I need to map my process variables into the service task but when I go to the
    implementation->use associations->simple, in the drag and drop plan is an object called a collection. I expand the collection and I get an object named recall. If I look at my datatypes my recall object shows the three attributes from the database schema for the table. (The datatype was created by the creation of the database adapter). However, I cannot expand the object in order to map in the individual process variables for the inputs for the insert parameters.
    Am I doing something incorrectly in attempting to map the process variables to the service call?
    Thanks,
    Christopher

    Christopher,
    Use the Transform in the Implementation tab instead of the Data Association to map your input process object to the service parameter. The Transform option creates an xslt file which you can use to map the input values to the database service values.
    Heidi.

  • Can someone explain what is the Database Instant Client

    Download 10.2.0.4 Database Instant Client for Apple Mac OS X on Intel x86
    What does the Database Instant Client contain?
    What functionality does it have?
    Does it include a 'lite' version of the database to run locally? Or is it only a set of tools that can connect to a remote (on another machine) database?

    It is only a client for connecting to databases. It is easier to install than the full client.
    It does not include a database. Check the following link :-
    http://www.oracle.com/technology/tech/oci/instantclient/index.html
    http://www.ContractOracle.com

  • Dirty Writes to the database

    I have a value object which was originally populated with information from database. I have a requirement to update ONLY the data that was changed in the value object. I could come up with something clever to do it myself, or I could use an existing pattern and Java API to accomplish this task. I prefer using an existing API.
    If there are any APIs out there that solve this problem, please let me know. Thanks in advance!

    I don't think that's the question here.
    I think the question is...
    Let's say four fields. One field gets updated.
    Only the one updated field gets updated to the
    database not a generic update for all four.Two situations.....
    1. I have a section of the application that updates
    ONLY that field. If so then I only update that
    field. There never can be a conflict. Consequently
    there is no need to discuss update strategies.
    2. I have a section of the application that updates
    that field and one other. Some other part of the
    application can update that other field. Given
    nothing more than that the two fields can be updated,
    there is no way to determine which one is correct.
    So you either find another rule or just use 'last
    t one wins' (which means discussing strategies for
    updates are meaningless.)I don't think we're talking about conflict resolution strategies. Well you are but I don't think the OP was. I think the OP is just talking about speeding up the serialization of an objects state to a database by not updating fields that haven't been modified.
    I don't disagree with what you are saying per se but I just don't see update conflict resolution listed as an issue by the OP.

  • Can multiple threads use same transaction concurrently?

              Is it possible that same transaction is being used by multiple threads concurrently
              in WLS? If each thread is doing suspend and resume how does it work? Does the
              transaction implementation support it? Is there a way to do it?
              

    Why you don't tell us some more about your application?
              I'm assuming this is a relatively long running transaction if run
              serially. One common solution is to break this type of workflow into
              several separate transactions with queues between them.
              -- Rob
              Karambir Singh wrote:
              > Is there any workaround for this? I mean to something like explicitly associating
              > the txn with user created threads.
              >
              > "krishna" <[email protected]> wrote:
              >
              >>Transaction context cannot be propagated to user created Threads.
              >>-Krishna
              >>"Karambir Singh" <[email protected]> wrote in message
              >>news:[email protected]...
              >>
              >>>I'm starting a transaction in main thread and this thread spawns three
              >>
              >>threads.
              >>
              >>>I want updates done in main transaction and updates done in three child
              >>
              >>threads
              >>
              >>>to be part of the same transaction. The transaction is finally commited
              >>
              >>by
              >>main
              >>
              >>>thread.
              >>>
              >>>
              >>>
              >>>
              >>>
              >>>"Dimitri I. Rakitine" <[email protected]> wrote:
              >>>
              >>>>What are you trying to do ?
              >>>>
              >>>>Karambir Singh <[email protected]> wrote:
              >>>>
              >>>>
              >>>>>Is there any workaround to do this?
              >>>>
              >>>>>"Dimitri I. Rakitine" <[email protected]> wrote:
              >>>>>
              >>>>>>No, it is associated with a thread which started it.
              >>>>>>
              >>>>>>Karambir Singh <[email protected]> wrote:
              >>>>>>
              >>>>>>
              >>>>>>>Is it possible that same transaction is being used by multiple
              >>
              >>threads
              >>
              >>>>>>concurrently
              >>>>>>
              >>>>>>>in WLS? If each thread is doing suspend and resume how does it
              >>
              >>work?
              >>
              >>>>>>Does the
              >>>>>>
              >>>>>>>transaction implementation support it? Is there a way to do it?
              >>>>>>
              >>>>>>--
              >>>>>>Dimitri
              >>>>>>
              >>>>
              >>>>--
              >>>>Dimitri
              >>>>
              >>>
              >>
              >
              

Maybe you are looking for