当前位置: 首页>>代码示例>>Java>>正文


Java Transaction类代码示例

本文整理汇总了Java中com.haulmont.cuba.core.Transaction的典型用法代码示例。如果您正苦于以下问题:Java Transaction类的具体用法?Java Transaction怎么用?Java Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Transaction类属于com.haulmont.cuba.core包,在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: assignLibraryDepartment

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public Collection<BookInstance> assignLibraryDepartment(Collection<BookInstance> bookInstances,
                                                        LibraryDepartment libraryDepartment, View bookInstanceView) {
    checkPermission(EntityOp.UPDATE);
    Collection<BookInstance> mergedInstances = new ArrayList<>();
    // Explicit transaction control
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        for (BookInstance booksInstance : bookInstances) {
            BookInstance instance = em.merge(booksInstance, bookInstanceView);
            instance.setLibraryDepartment(libraryDepartment);
            // Return the updated instance
            mergedInstances.add(instance);
        }
        tx.commit();
    }
    return mergedInstances;
}
 
开发者ID:cuba-platform,项目名称:old-library,代码行数:19,代码来源:BookInstanceServiceBean.java

示例2: testOneToOneLazy

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOneToOneLazy() {
    System.out.println("===================== BEGIN testOneToOneLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA2Id);
        assertNotNull(oneToOneA);
        assertNotNull(oneToOneA.getB());
        assertEquals(oneToOneA.getB().getId(), oneToOneB2Id);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToOneLazy =====================");
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:17,代码来源:SoftDeleteTest.java

示例3: authenticate

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
@Nonnull
public AuthenticationDetails authenticate(Credentials credentials) throws LoginException {
    checkNotNullArgument(credentials, "credentials should not be null");

    SecurityContext previousSecurityContext = AppContext.getSecurityContext();
    AppContext.setSecurityContext(new SecurityContext(serverSession));

    try (Transaction tx = persistence.getTransaction()) {
        AuthenticationDetails authenticationDetails = authenticateInternal(credentials);

        tx.commit();

        userSessionManager.clearPermissionsOnUser(authenticationDetails.getSession());

        return authenticationDetails;
    } finally {
        AppContext.setSecurityContext(previousSecurityContext);
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:21,代码来源:AuthenticationManagerBean.java

示例4: testOneToManyLazy

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOneToManyLazy() {
    System.out.println("===================== BEGIN testOneToManyLazy =====================");
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        User user = em.find(User.class, userId);

        List<UserRole> userRoles = user.getUserRoles();
        assertEquals(1, userRoles.size());
        for (UserRole ur : userRoles) {
            assertNotNull(ur.getRole());
        }

        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testOneToManyLazy =====================");
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:22,代码来源:SoftDeleteTest.java

示例5: setUp

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();

        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setPosition("manager");
        user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        em.persist(user);

        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setUser(user);
        userRole.setRole(em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93")));
        em.persist(userRole);

        tx.commit();
    } finally {
        tx.end();
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:26,代码来源:SerializationTest.java

示例6: storeAccessToken

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public void storeAccessToken(String tokenValue,
                             byte[] accessTokenBytes,
                             String authenticationKey,
                             byte[] authenticationBytes,
                             Date tokenExpiry,
                             String userLogin,
                             Locale locale,
                             String refreshTokenValue) {
    storeAccessTokenToMemory(tokenValue, accessTokenBytes, authenticationKey, authenticationBytes, tokenExpiry,
            userLogin, refreshTokenValue);
    if (serverConfig.getRestStoreTokensInDb()) {
        try (Transaction tx = persistence.getTransaction()) {
            removeAccessTokenFromDatabase(tokenValue);
            storeAccessTokenToDatabase(tokenValue, accessTokenBytes, authenticationKey, authenticationBytes,
                    tokenExpiry, userLogin, locale, refreshTokenValue);
            tx.commit();
        }
    }
    clusterManagerAPI.send(new TokenStoreAddAccessTokenMsg(tokenValue, accessTokenBytes, authenticationKey,
            authenticationBytes, tokenExpiry, userLogin, refreshTokenValue));
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:23,代码来源:ServerTokenStoreImpl.java

示例7: storeAccessTokenToDatabase

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected void storeAccessTokenToDatabase(String tokenValue,
                                          byte[] accessTokenBytes,
                                          String authenticationKey,
                                          byte[] authenticationBytes,
                                          Date tokenExpiry,
                                          String userLogin,
                                          @Nullable Locale locale,
                                          @Nullable String refreshTokenValue) {
    try (Transaction tx = persistence.getTransaction()) {
        EntityManager em = persistence.getEntityManager();
        AccessToken accessToken = metadata.create(AccessToken.class);
        accessToken.setCreateTs(timeSource.currentTimestamp());
        accessToken.setTokenValue(tokenValue);
        accessToken.setTokenBytes(accessTokenBytes);
        accessToken.setAuthenticationKey(authenticationKey);
        accessToken.setAuthenticationBytes(authenticationBytes);
        accessToken.setExpiry(tokenExpiry);
        accessToken.setUserLogin(userLogin);
        accessToken.setLocale(locale != null ? locale.toString() : null);
        accessToken.setRefreshTokenValue(refreshTokenValue);
        em.persist(accessToken);
        tx.commit();
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:25,代码来源:ServerTokenStoreImpl.java

示例8: testCreateEntity

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testCreateEntity() throws Exception {
    User u = metadata.create(User.class);
    u.setLogin("u-" + u.getId());
    u.setGroup(companyGroup);

    TestBeforeCommitTxListener.test = "testCreateEntity";
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(u);
        tx.commit();
    } finally {
        TestBeforeCommitTxListener.test = null;
    }

    try (Transaction tx = persistence.createTransaction()) {
        User user = persistence.getEntityManager().find(User.class, TestBeforeCommitTxListener.createdEntityId);
        assertNotNull(user);
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:20,代码来源:BeforeCommitTransactionListenerTest.java

示例9: migrateAttachmentsBatch

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected int migrateAttachmentsBatch() {
    List<SendingAttachment> resultList;
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String qstr = "select a from sys$SendingAttachment a where a.content is not null";
        TypedQuery<SendingAttachment> query = em.createQuery(qstr, SendingAttachment.class);
        query.setMaxResults(50);
        query.setViewName(View.MINIMAL);

        resultList = query.getResultList();

        tx.commit();
    } finally {
        tx.end();
    }

    if (!resultList.isEmpty()) {
        emailer.migrateAttachmentsToFileStorage(resultList);
    }

    return resultList.size();
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:24,代码来源:Emailer.java

示例10: testCommit

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testCommit() throws Exception {
    User u = metadata.create(User.class);
    u.setLogin("TxLstnrTst-1-" + u.getId());
    u.setGroup(companyGroup);
    TestAfterCompleteTxListener.test = "testCommit";
    try {
        try (Transaction tx = persistence.createTransaction()) {
            persistence.getEntityManager().persist(u);
            tx.commit();
        }
    } finally {
        TestAfterCompleteTxListener.test = null;
    }

    try (Transaction tx = persistence.createTransaction()) {
        User user = persistence.getEntityManager().find(User.class, u.getId());
        assertEquals("updated by TestAfterCompleteTxListener", user.getName());
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:21,代码来源:AfterCompleteTransactionListenerTest.java

示例11: testOpenTransaction

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testOpenTransaction() throws Exception {
    TestingService service = AppBeans.get(TestingService.class);
    appender.getMessages().clear();

    // programmatic tx without proper completion
    Object tx = service.leaveOpenTransaction();
    ((Transaction) tx).commit();
    assertEquals(1, appender.getMessages().stream().filter(s -> s.contains("Open transaction")).count());

    appender.getMessages().clear();

    // declarative tx
    service.declarativeTransaction();
    assertEquals(0, appender.getMessages().stream().filter(s -> s.contains("Open transaction")).count());
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:17,代码来源:ServiceInterceptorTest.java

示例12: testUpdateQuery_CleanupMode

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Test
public void testUpdateQuery_CleanupMode() {
    System.out.println("===================== BEGIN testUpdateQuery_CleanupMode =====================");

    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        em.setSoftDeletion(false);
        Query query = em.createQuery("update sec$Role r set r.description = ?1 where r.name = ?2");
        query.setParameter(1, "Updated");
        query.setParameter(2, "roleToBeDeleted");
        int updated = query.executeUpdate();

        assertEquals(1, updated);
        tx.commit();
    } finally {
        tx.end();
    }
    System.out.println("===================== END testUpdateQuery_CleanupMode =====================");
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:21,代码来源:SoftDeleteTest.java

示例13: loadContentText

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public String loadContentText(SendingMessage sendingMessage) {
    SendingMessage msg;
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        msg = em.reload(sendingMessage, "sendingMessage.loadContentText");
        tx.commit();
    }
    Objects.requireNonNull(msg, "Sending message not found: " + sendingMessage.getId());
    if (msg.getContentTextFile() != null) {
        byte[] bodyContent;
        try {
            bodyContent = fileStorage.loadFile(msg.getContentTextFile());
        } catch (FileStorageException e) {
            throw new RuntimeException(e);
        }
        //noinspection UnnecessaryLocalVariable
        String res = bodyTextFromByteArray(bodyContent);
        return res;
    } else {
        return msg.getContentText();
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:24,代码来源:Emailer.java

示例14: persistMessages

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
protected void persistMessages(List<SendingMessage> sendingMessageList, SendingStatus status) {
    MessagePersistingContext context = new MessagePersistingContext();

    try {
        try (Transaction tx = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            for (SendingMessage message : sendingMessageList) {
                message.setStatus(status);

                try {
                    persistSendingMessage(em, message, context);
                } catch (FileStorageException e) {
                    throw new RuntimeException("Failed to store message " + message.getCaption(), e);
                }
            }
            tx.commit();
        }
        context.finished();
    } finally {
        removeOrphanFiles(context);
    }
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:23,代码来源:Emailer.java

示例15: computeSecurityState

import com.haulmont.cuba.core.Transaction; //导入依赖的package包/类
@Override
public SecurityState computeSecurityState(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");

    SecurityState state;
    String storeName = metadataTools.getStoreName(metadata.getClassNN(entity.getClass()));
    Transaction tx = persistence.createTransaction(storeName);
    try {
        EntityManager em = persistence.getEntityManager(storeName);
        Entity managedEntity = em.merge(entity);
        support.setupAttributeAccess(managedEntity);
        state = BaseEntityInternalAccess.getSecurityState(managedEntity);
        // do not commit the transaction
    } finally {
        tx.end();
    }
    return state;
}
 
开发者ID:cuba-platform,项目名称:cuba,代码行数:19,代码来源:AttributeAccessServiceBean.java


注:本文中的com.haulmont.cuba.core.Transaction类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。