本文整理汇总了Java中org.springframework.transaction.TransactionException类的典型用法代码示例。如果您正苦于以下问题:Java TransactionException类的具体用法?Java TransactionException怎么用?Java TransactionException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TransactionException类属于org.springframework.transaction包,在下文中一共展示了TransactionException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resume
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
/**
* Resume the given transaction. Delegates to the {@code doResume}
* template method first, then resuming transaction synchronization.
* @param transaction the current transaction object
* @param resourcesHolder the object that holds suspended resources,
* as returned by {@code suspend} (or {@code null} to just
* resume synchronizations, if any)
* @see #doResume
* @see #suspend
*/
protected final void resume(Object transaction, SuspendedResourcesHolder resourcesHolder)
throws TransactionException {
if (resourcesHolder != null) {
Object suspendedResources = resourcesHolder.suspendedResources;
if (suspendedResources != null) {
doResume(transaction, suspendedResources);
}
List<TransactionSynchronization> suspendedSynchronizations = resourcesHolder.suspendedSynchronizations;
if (suspendedSynchronizations != null) {
TransactionSynchronizationManager.setActualTransactionActive(resourcesHolder.wasActive);
TransactionSynchronizationManager.setCurrentTransactionIsolationLevel(resourcesHolder.isolationLevel);
TransactionSynchronizationManager.setCurrentTransactionReadOnly(resourcesHolder.readOnly);
TransactionSynchronizationManager.setCurrentTransactionName(resourcesHolder.name);
doResumeSynchronization(suspendedSynchronizations);
}
}
}
示例2: commit
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
/**
* This implementation of commit handles participating in existing
* transactions and programmatic rollback requests.
* Delegates to {@code isRollbackOnly}, {@code doCommit}
* and {@code rollback}.
* @see org.springframework.transaction.TransactionStatus#isRollbackOnly()
* @see #doCommit
* @see #rollback
*/
@Override
public final void commit(TransactionStatus status) throws TransactionException {
if (status.isCompleted()) {
throw new IllegalTransactionStateException(
"Transaction is already completed - do not call commit or rollback more than once per transaction");
}
DefaultTransactionStatus defStatus = (DefaultTransactionStatus) status;
if (defStatus.isLocalRollbackOnly()) {
if (defStatus.isDebug()) {
logger.debug("Transactional code has requested rollback");
}
processRollback(defStatus);
return;
}
if (!shouldCommitOnGlobalRollbackOnly() && defStatus.isGlobalRollbackOnly()) {
if (defStatus.isDebug()) {
logger.debug("Global transaction is marked as rollback-only but transactional code requested commit");
}
processRollback(defStatus);
// Throw UnexpectedRollbackException only at outermost transaction boundary
// or if explicitly asked to.
if (status.isNewTransaction() || isFailEarlyOnGlobalRollbackOnly()) {
throw new UnexpectedRollbackException(
"Transaction rolled back because it has been marked as rollback-only");
}
return;
}
processCommit(defStatus);
}
示例3: getTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
Transaction.IsolationLevel isolationLevel = translateIsolationLevel(definition.getIsolationLevel());
Transaction t;
if (isolationLevel == null) {
t = sql.transaction();
} else {
t = sql.transaction(isolationLevel);
}
if (definition.isReadOnly()) {
Wrap.execute(() -> t.getConnection().setReadOnly(true));
}
return new SqlStreamTransactionStatus(t);
}
示例4: startNewTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
/**
* Start a new transaction. Only call this method if {@link #endTransaction()} has been called.
* {@link #setComplete()} can be used again in the new transaction. The fate of the new transaction, by default,
* will be the usual rollback.
*
* @throws TransactionException if starting the transaction failed
*/
protected void startNewTransaction() throws TransactionException
{
if (this.transactionStatus != null)
{
throw new IllegalStateException("Cannot start new transaction without ending existing transaction: "
+ "Invoke endTransaction() before startNewTransaction()");
}
if (this.transactionManager == null)
{
throw new IllegalStateException("No transaction manager set");
}
this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition);
++this.transactionsStarted;
this.complete = !this.isRollback();
}
示例5: beginTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
throws PersistenceException, SQLException, TransactionException {
OpenJPAEntityManager openJpaEntityManager = getOpenJPAEntityManager(entityManager);
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
// Pass custom isolation level on to OpenJPA's JDBCFetchPlan configuration
FetchPlan fetchPlan = openJpaEntityManager.getFetchPlan();
if (fetchPlan instanceof JDBCFetchPlan) {
IsolationLevel isolation = IsolationLevel.fromConnectionConstant(definition.getIsolationLevel());
((JDBCFetchPlan) fetchPlan).setIsolation(isolation);
}
}
entityManager.getTransaction().begin();
if (!definition.isReadOnly()) {
// Like with EclipseLink, make sure to start the logic transaction early so that other
// participants using the connection (such as JdbcTemplate) run in a transaction.
openJpaEntityManager.beginStore();
}
// Custom implementation for OpenJPA savepoint handling
return new OpenJpaTransactionData(openJpaEntityManager);
}
示例6: beginTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
throws PersistenceException, SQLException, TransactionException {
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
// Pass custom isolation level on to EclipseLink's DatabaseLogin configuration
// (since Spring 4.1.2)
UnitOfWork uow = entityManager.unwrap(UnitOfWork.class);
uow.getLogin().setTransactionIsolation(definition.getIsolationLevel());
}
entityManager.getTransaction().begin();
if (!definition.isReadOnly() && !this.lazyDatabaseTransaction) {
// Begin an early transaction to force EclipseLink to get a JDBC Connection
// so that Spring can manage transactions with JDBC as well as EclipseLink.
entityManager.unwrap(UnitOfWork.class).beginEarlyTransaction();
}
return null;
}
示例7: startNewTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
/**
* Start a new transaction. Only call this method if
* {@link #endTransaction()} has been called. {@link #setComplete()} can be
* used again in the new transaction. The fate of the new transaction, by
* default, will be the usual rollback.
* @throws TransactionException if starting the transaction failed
*/
protected void startNewTransaction() throws TransactionException {
if (this.transactionStatus != null) {
throw new IllegalStateException("Cannot start new transaction without ending existing transaction: "
+ "Invoke endTransaction() before startNewTransaction()");
}
if (this.transactionManager == null) {
throw new IllegalStateException("No transaction manager set");
}
this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition);
++this.transactionsStarted;
this.complete = !this.isRollback();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Began transaction (" + this.transactionsStarted + "): transaction manager ["
+ this.transactionManager + "]; rollback [" + this.isRollback() + "].");
}
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:AbstractTransactionalSpringContextTests.java
示例8: beginTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
public Object beginTransaction(final EntityManager entityManager,
final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException {
Session session = (Session) entityManager.getDelegate();
if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
getSession(entityManager).getTransaction().setTimeout(definition.getTimeout());
}
entityManager.getTransaction().begin();
logger.debug("Transaction started");
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
logger.debug("The connection instance is " + connection.toString());
logger.debug("The isolation level of the connection is " + connection.getTransactionIsolation()
+ " and the isolation level set on the transaction is " + definition.getIsolationLevel() );
DataSourceUtils.prepareConnectionForTransaction(connection, definition);
}
});
return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
}
示例9: doGetTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
protected Object doGetTransaction() throws TransactionException {
JiniTransactionObject txObject = new JiniTransactionObject();
// txObject.setNestedTransactionAllowed
// txObject.setJiniHolder(transactionalContext);
// set the jini holder is one is found
JiniTransactionHolder jiniHolder = (JiniTransactionHolder) TransactionSynchronizationManager.getResource(transactionalContext);
if (jiniHolder == null) {
jiniHolder = (JiniTransactionHolder) TransactionSynchronizationManager.getResource(ExistingJiniTransactionManager.CONTEXT);
}
if (jiniHolder != null) {
if (logger.isTraceEnabled()) {
logger.trace(logMessage("Found thread-bound tx data [" + jiniHolder + "] for Jini resource ["
+ transactionalContext + "]"));
}
txObject.setJiniHolder(jiniHolder, false);
}
return txObject;
}
示例10: createActionsForRolloutGroup
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
private long createActionsForRolloutGroup(final Rollout rollout, final RolloutGroup group) {
long totalActionsCreated = 0;
try {
long actionsCreated;
do {
actionsCreated = createActionsForTargetsInNewTransaction(rollout.getId(), group.getId(),
TRANSACTION_TARGETS);
totalActionsCreated += actionsCreated;
} while (actionsCreated > 0);
} catch (final TransactionException e) {
LOGGER.warn("Transaction assigning Targets to RolloutGroup failed", e);
return 0;
}
return totalActionsCreated;
}
示例11: onSessionEnd
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
/** {@inheritDoc} */
@Override public void onSessionEnd(CacheStoreSession ses, boolean commit) {
if (ses.isWithinTransaction()) {
TransactionStatus tx = ses.attach(null);
if (tx != null) {
try {
if (commit)
txMgr.commit(tx);
else
txMgr.rollback(tx);
}
catch (TransactionException e) {
throw new CacheWriterException("Failed to end store session [tx=" + ses.transaction() + ']', e);
}
}
}
}
示例12: test
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Test
public void test() {
try {
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
Tag hibernate = new Tag();
hibernate.setName("hibernate");
entityManager.persist(hibernate);
Tag jpa = new Tag();
jpa.setName("jpa");
entityManager.persist(jpa);
return null;
});
} catch (TransactionException e) {
LOGGER.error("Failure", e);
}
Post post = forumService.newPost("High-Performance Java Persistence", "hibernate", "jpa");
assertNotNull(post.getId());
}
开发者ID:vladmihalcea,项目名称:high-performance-java-persistence,代码行数:21,代码来源:JTATransactionManagerTest.java
示例13: init
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Before
public void init() {
try {
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
Tag hibernate = new Tag();
hibernate.setName("hibernate");
sessionFactory.getCurrentSession().persist(hibernate);
Tag jpa = new Tag();
jpa.setName("jpa");
sessionFactory.getCurrentSession().persist(jpa);
return null;
});
} catch (TransactionException e) {
LOGGER.error("Failure", e);
}
}
开发者ID:vladmihalcea,项目名称:high-performance-java-persistence,代码行数:19,代码来源:HibernateTransactionManagerTest.java
示例14: test
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Test
public void test() {
try {
transactionTemplate.execute( (TransactionCallback<Void>) transactionStatus -> {
SmsNotification sms = new SmsNotification();
sms.setPhoneNumber( "012-345-67890" );
sms.setFirstName( "Vlad" );
sms.setLastName( "Mihalcea" );
entityManager.persist( sms );
EmailNotification email = new EmailNotification();
email.setEmailAddress( "[email protected]" );
email.setFirstName( "Vlad" );
email.setLastName( "Mihalcea" );
entityManager.persist( email );
return null;
} );
}
catch (TransactionException e) {
LOGGER.error( "Failure", e );
}
notificationService.sendCampaign( "Black Friday", "High-Performance Java Persistence is 40% OFF" );
}
开发者ID:vladmihalcea,项目名称:high-performance-java-persistence,代码行数:27,代码来源:BehaviorDrivenInheritanceTest.java
示例15: beginTransaction
import org.springframework.transaction.TransactionException; //导入依赖的package包/类
@Override
public Object beginTransaction(EntityManager entityManager,
final TransactionDefinition definition) throws PersistenceException,
SQLException, TransactionException {
Session session = entityManager.unwrap(Session.class);
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
DataSourceUtils.prepareConnectionForTransaction(connection, definition);
if (connection.isReadOnly() && !definition.isReadOnly()) {
connection.setReadOnly(false);
}
}
});
entityManager.getTransaction().begin();
return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName());
}