Java源码示例:com.google.inject.persist.Transactional
示例1
@Transactional
void doUpdate(boolean optional, String workspaceId, Consumer<WorkspaceActivity> updater) {
EntityManager em = managerProvider.get();
WorkspaceActivity activity = em.find(WorkspaceActivity.class, workspaceId);
if (activity == null) {
if (optional) {
return;
}
activity = new WorkspaceActivity();
activity.setWorkspaceId(workspaceId);
updater.accept(activity);
em.persist(activity);
} else {
updater.accept(activity);
em.merge(activity);
}
em.flush();
}
示例2
@Override
@Transactional
public WorkspaceImpl get(String name, String namespace)
throws NotFoundException, ServerException {
requireNonNull(name, "Required non-null name");
requireNonNull(namespace, "Required non-null namespace");
try {
return new WorkspaceImpl(
managerProvider
.get()
.createNamedQuery("Workspace.getByName", WorkspaceImpl.class)
.setParameter("namespace", namespace)
.setParameter("name", name)
.getSingleResult());
} catch (NoResultException noResEx) {
throw new NotFoundException(
format("Workspace with name '%s' in namespace '%s' doesn't exist", name, namespace));
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例3
@Override
@Transactional
public Map<String, String> getPreferences(String userId, String filter) throws ServerException {
requireNonNull(userId);
requireNonNull(filter);
try {
final EntityManager manager = managerProvider.get();
final PreferenceEntity prefs = manager.find(PreferenceEntity.class, userId);
if (prefs == null) {
return new HashMap<>();
}
final Map<String, String> preferences = prefs.getPreferences();
if (!filter.isEmpty()) {
final Pattern pattern = Pattern.compile(filter);
return preferences
.entrySet()
.stream()
.filter(preference -> pattern.matcher(preference.getKey()).matches())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} else {
return preferences;
}
} catch (RuntimeException ex) {
throw new ServerException(ex.getLocalizedMessage(), ex);
}
}
示例4
@Override
@Transactional
public WorkspaceImpl get(String name, String namespace)
throws NotFoundException, ServerException {
requireNonNull(name, "Required non-null name");
requireNonNull(namespace, "Required non-null namespace");
try {
return new WorkspaceImpl(
managerProvider
.get()
.createNamedQuery("Workspace.getByName", WorkspaceImpl.class)
.setParameter("namespace", namespace)
.setParameter("name", name)
.getSingleResult());
} catch (NoResultException noResEx) {
throw new NotFoundException(
format("Workspace with name '%s' in namespace '%s' doesn't exist", name, namespace));
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例5
@Transactional
protected void doUpdateMachineStatus(String workspaceId, String machineName, MachineStatus status)
throws InfrastructureException {
EntityManager entityManager = managerProvider.get();
KubernetesMachineImpl machine =
entityManager.find(KubernetesMachineImpl.class, new MachineId(workspaceId, machineName));
if (machine == null) {
throw new InfrastructureException(
format("Machine '%s:%s' was not found", workspaceId, machineName));
}
machine.setStatus(status);
entityManager.flush();
}
示例6
@Override
@Transactional
public List<SshPairImpl> get(String owner, String service) throws ServerException {
requireNonNull(owner);
requireNonNull(service);
try {
return managerProvider
.get()
.createNamedQuery("SshKeyPair.getByOwnerAndService", SshPairImpl.class)
.setParameter("owner", owner)
.setParameter("service", service)
.getResultList();
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}
示例7
@Override
@Transactional
public Page<FreeResourcesLimitImpl> getAll(int maxItems, int skipCount) throws ServerException {
try {
final List<FreeResourcesLimitImpl> list =
managerProvider
.get()
.createNamedQuery("FreeResourcesLimit.getAll", FreeResourcesLimitImpl.class)
.setMaxResults(maxItems)
.setFirstResult(skipCount)
.getResultList()
.stream()
.map(FreeResourcesLimitImpl::new)
.collect(Collectors.toList());
return new Page<>(list, skipCount, maxItems, getTotalCount());
} catch (RuntimeException e) {
throw new ServerException(e.getMessage(), e);
}
}
示例8
@Override
@Transactional(rollbackOn = ServerException.class)
public long countWorkspacesInStatus(WorkspaceStatus status, long timestamp)
throws ServerException {
try {
String queryName = "WorkspaceActivity.get" + firstUpperCase(status.name()) + "SinceCount";
return managerProvider
.get()
.createNamedQuery(queryName, Long.class)
.setParameter("time", timestamp)
.getSingleResult();
} catch (RuntimeException e) {
throw new ServerException(e.getMessage(), e);
}
}
示例9
@Override
@Transactional
public OrganizationImpl getById(String organizationId) throws NotFoundException, ServerException {
requireNonNull(organizationId, "Required non-null organization id");
try {
final EntityManager manager = managerProvider.get();
OrganizationImpl organization = manager.find(OrganizationImpl.class, organizationId);
if (organization == null) {
throw new NotFoundException(
format("Organization with id '%s' doesn't exist", organizationId));
}
return organization;
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}
示例10
@Override
@Transactional
public OrganizationDistributedResourcesImpl get(String organizationId)
throws NotFoundException, ServerException {
requireNonNull(organizationId, "Required non-null organization id");
try {
OrganizationDistributedResourcesImpl distributedResources =
managerProvider.get().find(OrganizationDistributedResourcesImpl.class, organizationId);
if (distributedResources == null) {
throw new NotFoundException(
"There are no distributed resources for organization with id '"
+ organizationId
+ "'.");
}
return new OrganizationDistributedResourcesImpl(distributedResources);
} catch (RuntimeException e) {
throw new ServerException(e.getMessage(), e);
}
}
示例11
@Override
@Transactional
public List<MemberImpl> getMemberships(String userId) throws ServerException {
requireNonNull(userId, "Required non-null user id");
try {
final EntityManager manager = managerProvider.get();
return manager
.createNamedQuery("Member.getByUser", MemberImpl.class)
.setParameter("userId", userId)
.getResultList()
.stream()
.map(MemberImpl::new)
.collect(toList());
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}
示例12
@Override
@Transactional
public SignatureKeyPairImpl get(String workspaceId) throws NotFoundException, ServerException {
final EntityManager manager = managerProvider.get();
try {
return new SignatureKeyPairImpl(
manager
.createNamedQuery("SignKeyPair.getAll", SignatureKeyPairImpl.class)
.setParameter("workspaceId", workspaceId)
.getSingleResult());
} catch (NoResultException x) {
throw new NotFoundException(
format("Signature key pair for workspace '%s' doesn't exist", workspaceId));
} catch (RuntimeException ex) {
throw new ServerException(ex.getMessage(), ex);
}
}
示例13
@Transactional(rollbackOn = {RuntimeException.class, ServerException.class})
protected void doRemove(RuntimeIdentity runtimeIdentity) throws ServerException {
EntityManager em = managerProvider.get();
KubernetesRuntimeState runtime =
em.find(KubernetesRuntimeState.class, runtimeIdentity.getWorkspaceId());
if (runtime != null) {
eventService
.publish(
new BeforeKubernetesRuntimeStateRemovedEvent(new KubernetesRuntimeState(runtime)))
.propagateException();
em.remove(runtime);
}
}
示例14
@Override
@Transactional
public AccountImpl getById(String id) throws NotFoundException, ServerException {
requireNonNull(id, "Required non-null account id");
final EntityManager manager = managerProvider.get();
try {
AccountImpl account = manager.find(AccountImpl.class, id);
if (account == null) {
throw new NotFoundException(format("Account with id '%s' was not found", id));
}
return account;
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例15
@Transactional
void dropData() {
all(OrderLine.class).delete();
all(Order.class).delete();
all(Product.class).delete();
all(Address.class).delete();
all(Email.class).delete();
all(Contact.class).delete();
all(Country.class).delete();
all(Circle.class).delete();
all(Title.class).delete();
}
示例16
@Transactional
void createSequence() {
MetaSequence sequence = new MetaSequence(SEQUENCE_NAME);
sequence.setInitial(1L);
sequence.setIncrement(1);
sequence.setPadding(5);
sequence.setPrefix("SO");
Beans.get(MetaSequenceRepository.class).save(sequence);
}
示例17
@Transactional
protected Country createDefaultCountry() {
log.debug("creating a Country record: { code: \"FR\", name: \"France\"}");
Country frCountry = new Country("FR", "France");
return Beans.get(CountryRepository.class).save(frCountry);
}
示例18
@Transactional
protected FactoryImpl doUpdate(FactoryImpl update) throws NotFoundException {
final EntityManager manager = managerProvider.get();
if (manager.find(FactoryImpl.class, update.getId()) == null) {
throw new NotFoundException(
format("Could not update factory with id %s because it doesn't exist", update.getId()));
}
if (update.getWorkspace() != null) {
update.getWorkspace().getProjects().forEach(ProjectConfigImpl::prePersistAttributes);
}
FactoryImpl merged = manager.merge(update);
manager.flush();
return merged;
}
示例19
@Override
@Transactional
public long getTotalCount() throws ServerException {
try {
return managerProvider
.get()
.createNamedQuery("User.getTotalCount", Long.class)
.getSingleResult();
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例20
@Transactional
protected void doRemove(String userId) {
final EntityManager manager = managerProvider.get();
final PreferenceEntity prefs = manager.find(PreferenceEntity.class, userId);
if (prefs != null) {
manager.remove(prefs);
manager.flush();
}
}
示例21
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void saveAll(Path filePath, Config config) {
KinopoiskFile existingFile = kinopoiskFileService.find(filePath);
if (existingFile == null) {
existingFile = kinopoiskFileService.save(filePath);
}
List<Movie> movies = movieService.saveOrUpdateAll(filePath);
importProgressRepository.saveAll(existingFile, movies, config);
}
示例22
/**
* {@inheritDoc}
*/
@Override
@Transactional
public void deleteAll(Path filePath) {
KinopoiskFile existingFile = kinopoiskFileService.find(filePath);
if (existingFile == null) {
return;
}
kinopoiskFileService.delete(filePath);
importProgressRepository.deleteAll(existingFile);
}
示例23
@Transactional
protected void doRemove(String userId) {
final EntityManager manager = managerProvider.get();
final ProfileImpl profile = manager.find(ProfileImpl.class, userId);
if (profile != null) {
manager.remove(profile);
manager.flush();
}
}
示例24
@Transactional
@Override
public Page<InviteImpl> getInvites(String email, int maxItems, long skipCount)
throws ServerException {
requireNonNull(email, "Required non-null email");
checkArgument(
skipCount <= Integer.MAX_VALUE,
"The number of items to skip can't be greater than " + Integer.MAX_VALUE);
try {
final EntityManager manager = managerProvider.get();
List<InviteImpl> result =
managerProvider
.get()
.createNamedQuery("Invite.getByEmail", InviteImpl.class)
.setParameter("email", email)
.setFirstResult((int) skipCount)
.setMaxResults(maxItems)
.getResultList()
.stream()
.map(InviteImpl::new)
.collect(Collectors.toList());
final long invitesCount =
manager
.createNamedQuery("Invite.getByEmailCount", Long.class)
.setParameter("email", email)
.getSingleResult();
return new Page<>(result, skipCount, maxItems, invitesCount);
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}
示例25
@Transactional
@Override
public Page<InviteImpl> getInvites(
String domainId, String instanceId, long skipCount, int maxItems) throws ServerException {
requireNonNull(domainId, "Required non-null domain");
requireNonNull(instanceId, "Required non-null instance");
checkArgument(
skipCount <= Integer.MAX_VALUE,
"The number of items to skip can't be greater than " + Integer.MAX_VALUE);
try {
final EntityManager manager = managerProvider.get();
List<InviteImpl> result =
managerProvider
.get()
.createNamedQuery("Invite.getByInstance", InviteImpl.class)
.setParameter("domain", domainId)
.setParameter("instance", instanceId)
.setFirstResult((int) skipCount)
.setMaxResults(maxItems)
.getResultList()
.stream()
.map(InviteImpl::new)
.collect(Collectors.toList());
final long invitationsCount =
manager
.createNamedQuery("Invite.getByInstanceCount", Long.class)
.setParameter("domain", domainId)
.setParameter("instance", instanceId)
.getSingleResult();
return new Page<>(result, skipCount, maxItems, invitationsCount);
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}
示例26
@Transactional(rollbackOn = {RuntimeException.class, ServerException.class})
protected Optional<WorkspaceImpl> doRemove(String id) throws ServerException {
final WorkspaceImpl workspace = managerProvider.get().find(WorkspaceImpl.class, id);
if (workspace == null) {
return Optional.empty();
}
final EntityManager manager = managerProvider.get();
eventService
.publish(new BeforeWorkspaceRemovedEvent(new WorkspaceImpl(workspace)))
.propagateException();
manager.remove(workspace);
manager.flush();
return Optional.of(workspace);
}
示例27
@Transactional
protected void doRemove(String domain, String instance, String email) {
try {
InviteImpl invite = getEntity(domain, instance, email);
final EntityManager manager = managerProvider.get();
manager.remove(invite);
manager.flush();
} catch (NotFoundException e) {
// invite is already removed
}
}
示例28
@Override
@Transactional
public Page<WorkspaceImpl> getByNamespace(String namespace, int maxItems, long skipCount)
throws ServerException {
requireNonNull(namespace, "Required non-null namespace");
try {
final EntityManager manager = managerProvider.get();
final List<WorkspaceImpl> list =
manager
.createNamedQuery("Workspace.getByNamespace", WorkspaceImpl.class)
.setParameter("namespace", namespace)
.setMaxResults(maxItems)
.setFirstResult((int) skipCount)
.getResultList()
.stream()
.map(WorkspaceImpl::new)
.collect(Collectors.toList());
final long count =
manager
.createNamedQuery("Workspace.getByNamespaceCount", Long.class)
.setParameter("namespace", namespace)
.getSingleResult();
return new Page<>(list, skipCount, maxItems, count);
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例29
@Override
@Transactional
public Page<UserImpl> getByNamePart(String namePart, int maxItems, long skipCount)
throws ServerException {
requireNonNull(namePart, "Required non-null name part");
checkArgument(maxItems >= 0, "The number of items to return can't be negative");
checkArgument(
skipCount >= 0 && skipCount <= Integer.MAX_VALUE,
"The number of items to skip can't be negative or greater than " + Integer.MAX_VALUE);
try {
final List<UserImpl> list =
managerProvider
.get()
.createNamedQuery("User.getByNamePart", UserImpl.class)
.setParameter("name", namePart.toLowerCase())
.setMaxResults(maxItems)
.setFirstResult((int) skipCount)
.getResultList()
.stream()
.map(JpaUserDao::erasePassword)
.collect(toList());
final long count =
managerProvider
.get()
.createNamedQuery("User.getByNamePartCount", Long.class)
.setParameter("name", namePart.toLowerCase())
.getSingleResult();
return new Page<>(list, skipCount, maxItems, count);
} catch (RuntimeException x) {
throw new ServerException(x.getLocalizedMessage(), x);
}
}
示例30
@Override
@Transactional
public Page<WorkerImpl> getByInstance(String instanceId, int maxItems, long skipCount)
throws ServerException {
requireNonNull(instanceId, "Workspace identifier required");
checkArgument(
skipCount <= Integer.MAX_VALUE,
"The number of items to skip can't be greater than " + Integer.MAX_VALUE);
try {
final EntityManager entityManager = managerProvider.get();
final List<WorkerImpl> workers =
entityManager
.createNamedQuery("Worker.getByWorkspaceId", WorkerImpl.class)
.setParameter("workspaceId", instanceId)
.setMaxResults(maxItems)
.setFirstResult((int) skipCount)
.getResultList()
.stream()
.map(WorkerImpl::new)
.collect(toList());
final Long workersCount =
entityManager
.createNamedQuery("Worker.getCountByWorkspaceId", Long.class)
.setParameter("workspaceId", instanceId)
.getSingleResult();
return new Page<>(workers, skipCount, maxItems, workersCount);
} catch (RuntimeException e) {
throw new ServerException(e.getLocalizedMessage(), e);
}
}