Use sessions to interact with data, including insert and
update operations.
About this task
The ObjectMap interface
has the typical Map operations such as put, get, and remove. However,
use the more specific operation names such as: get, getForUpdate,
insert, update, and remove. These method names convey the intent more
precisely than the traditional Map APIs.
Note: The
upsert and
upsertAll methods
replace the ObjectMap
put and
putAll methods.
Use the
upsert method to tell the BackingMap
and loader that an entry in the data grid needs
to place the key and value into the grid. The BackingMap
and loader does either an insert or an update
to place the value into the grid
and loader .
If you run the upsert API
within your applications, then the loader gets an UPSERT LogElement
type, which allows loaders to do database merge or upsert calls instead
of using insert or update.
You
can also use the indexing support, which is flexible.
Procedure
- Insert data.
After you obtain a session,
you can use the following code fragment to use the Map API for inserting
data.
Session session = ...;
ObjectMap personMap = session.getMap("PERSON");
session.begin();
Person p = new Person();
p.name = "John Doe";
personMap.insert(p.name, p);
session.commit();
The same example using the EntityManager API follows. This code sample assumes
that the Person object is mapped to an Entity.
Session session = ...;
EntityManager em = session.getEntityManager();
session.begin();
Person p = new Person();
p.name = "John Doe";
em.persist(p);
session.commit();
The pattern is designed to obtain
references to the ObjectMaps for the Maps that the thread works with,
start a transaction, work with the data, then commit the transaction.
- Update data.
Use the following code fragment
to use the Map API for updating data.
session.begin();
Person p = (Person)personMap.getForUpdate("John Doe");
p.name = "John Doe";
p.age = 30;
personMap.update(p.name, p);
session.commit();
The application normally uses the getForUpdate method rather than a simple get
to lock the record. The update method must be called to actually provide the updated value to the
Map. If update is not called then the Map is unchanged. The following code is
the same fragment using the EntityManager API:
session.begin();
Person p = (Person)em.findForUpdate(Person.class, "John Doe");
p.age = 30;
session.commit();
The EntityManager
API is simpler than the Map approach. In this case, eXtreme Scale finds
the Entity and returns a managed object to the application. The application
modifies the object and commits the transaction, and eXtreme Scale tracks
changes to managed objects automatically at commit time and performs
the necessary updates.
-
Insert data with the two-phase commitment protocol by calling the following method:
session.setTxCommitProtocol(Session.TxCommitProtocol.TWOPHASE); session.begin();
The following code snippet illustrates how to create, retrieve, update, and delete operations in a
grid with a two-phase commit protocol.
Session session = og.getSession();
Objectmap map1 = session.getMap("Map1");
Objectmap map2 = session.getMap("Map2");
Objectmap map3 = session.getMap("Map3");
session.setTxCommitProtocol(Session.TxCommitProtocol.TWOPHASE);
session.begin();
map1.insert("randKey345", "HelloMap1");
map2.insert("randKey58901", "HelloMap2");
map3.insert("randKey58", "HelloMap3");
session.commit();