@Override
public V create() {
boolean newGroup = false;
boolean success = false;
UUID group = CURRENT_GROUP.get();
Batch batch = this.manager.getBatcher().createBatch();
try {
if (group == null) {
newGroup = true;
group = this.manager.getGroupIdentifierFactory().createIdentifier();
CURRENT_GROUP.set(group);
}
try {
// This will invoke Cache.create() for nested beans
// Nested beans will share the same group identifier
V instance = this.factory.createInstance();
K id = instance.getId();
this.manager.createBean(id, group, instance).close();
success = true;
return instance;
} finally {
if (newGroup) {
CURRENT_GROUP.remove();
}
}
} finally {
if (success) {
batch.close();
} else {
batch.discard();
}
}
}