本文整理了Java中org.hibernate.Transaction.isActive()
方法的一些代码示例,展示了Transaction.isActive()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Transaction.isActive()
方法的具体详情如下:
包路径:org.hibernate.Transaction
类名称:Transaction
方法名:isActive
[英]Is this transaction still active?
Answers on a best effort basis. For example, in the case of JDBC based transactions we cannot know that a transaction is active when it is initiated directly through the JDBC java.sql.Connection, only when it is initiated from here.
[中]该交易仍在进行中吗?
尽最大努力回答问题。例如,在基于JDBC的事务的情况下,当事务直接通过JDBC java启动时,我们无法知道它是否处于活动状态。sql。连接,仅当从此处启动时。
代码示例来源:origin: jooby-project/jooby
protected void active(final Session session, final Consumer<Transaction> active,
final Consumer<Transaction> inactive) {
Transaction trx = session.getTransaction();
if (trx.isActive()) {
active.accept(trx);
} else {
inactive.accept(trx);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test(expected = IllegalStateException.class)
public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() throws Exception {
try (Session s = openSession()) {
tm.begin();
Transaction tx = null;
try {
// A call to begin() with an active Tx should cause an IllegalStateException
tx = s.beginTransaction();
}
catch (Exception e) {
if ( tx != null && tx.isActive() ) {
tx.rollback();
}
throw e;
}
}
catch (Exception e) {
if ( tm.getStatus() == Status.STATUS_ACTIVE ) {
tm.rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void prepareTest() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
try {
session.save( new MyEntity( 1L, "entity_1", new BigInteger( "3" ) ) );
session.save( new MyEntity( 2L, "entity_2", new BigInteger( "6" ) ) );
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void prepareTest() throws Exception {
try (Session session = openSession()) {
session.beginTransaction();
try {
for ( int i = 0; i < 20; i++ ) {
Person p1 = new Person( i, "p" + i );
session.save( p1 );
}
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void prepareTest() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
try {
session.save( new MyEntity( 1L, "entity_1" ) );
session.save( new MyEntity( 2L, "entity_2" ) );
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void cleanupTest() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
try {
session.createQuery( "delete from MyEntity" ).executeUpdate();
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void cleanupTest() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
try {
session.createQuery( "delete from MyEntity" ).executeUpdate();
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void cleanupTestData() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
try {
session.createQuery( "delete from TestEntity" ).executeUpdate();
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
/**
* Tests that lazy loading without transaction nor open session is generally
* working. The magic is done by {@link AbstractLazyInitializer} who opens a
* temporary session.
*/
@SuppressWarnings("unchecked")
@Test
public void testProxyInitializationWithoutTX() {
final Session s = openSession();
final Transaction t = s.beginTransaction();
try {
final Map<String, Object> child = (Map<String, Object>) s.load( "ChildEntity", 1L );
final Map<String, Object> parent = (Map<String, Object>) child.get( "parent" );
t.rollback();
session.close();
// assert we have an uninitialized proxy
assertTrue( parent instanceof MapProxy );
assertFalse( Hibernate.isInitialized( parent ) );
assertEquals( "TheParent", parent.get( "name" ) );
// assert we have an initialized proxy now
assertTrue( Hibernate.isInitialized( parent ) );
}
finally {
if ( t.isActive() ) {
t.rollback();
}
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@After
public void tearDown() {
try (Session s = openSession()) {
session.getTransaction().begin();
try {
s.createQuery( "delete from TestEntity" ).executeUpdate();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
private void insertTestEntity(String name) {
final TestEntity entity = new TestEntity();
entity.setName( name );
try (Session s = openSession()) {
session.getTransaction().begin();
try {
s.save( entity );
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test(expected = IllegalStateException.class)
public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() {
Transaction transaction = null;
try (Session session = openSession()) {
transaction = session.getTransaction();
transaction.begin();
// A call to begin() with an active Tx should cause an IllegalStateException
transaction.begin();
}
finally {
if ( transaction != null && transaction.isActive() ) {
transaction.rollback();
}
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-11182")
public void testSubQueryConstraintPropertyInSuperclassTable() {
Session s = openSession();
try {
s.getTransaction().begin();
// employee.firstName is in Person table (not Employee)
String queryHQL = "from InvestmentCompany investmentCompany "
+ "where exists "
+ "(select employee "
+ "from investmentCompany.employees as employee "
+ " where employee.firstName = 'Joe')";
s.createQuery( queryHQL ).uniqueResult();
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
finally {
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Override
protected void prepareTest() throws Exception {
try (Session session = openSession()) {
session.getTransaction().begin();
TestEntity entity = new TestEntity();
entity.setDate( new DateAttribute( System.currentTimeMillis() ) );
try {
session.persist( entity );
session.getTransaction().commit();
}
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-11182")
public void testSubQueryConstraintPropertyInEntityTable() {
Session s = openSession();
try {
s.getTransaction().begin();
// employee.employeeNumber is in Employee table
String queryHQL = "from InvestmentCompany investmentCompany "
+ "where exists "
+ "(select employee "
+ "from investmentCompany.employees as employee "
+ " where employee.employeeNumber = 666 )";
s.createQuery( queryHQL ).uniqueResult();
}
catch (Exception e) {
if ( s.getTransaction() != null && s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
finally {
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testUpdateRequiresTxn() {
inSession(
session -> {
try {
assertFalse( session.getTransaction().isActive() );
// Query
session.createQuery( "update Person set name = 'steve'" ).executeUpdate();
fail( "expecting failure" );
}
catch (TransactionRequiredException expected) {
// expected condition
}
}
);
}
代码示例来源:origin: hibernate/hibernate-orm
/**
* Tests that lazy loading without transaction nor open session is generally
* working. The magic is done by {@link AbstractLazyInitializer} who opens a
* temporary session.
*/
@SuppressWarnings("unchecked")
@Test
public void testProxyInitializationWithoutTX() {
final Session s = openSession();
final Transaction t = s.beginTransaction();
try {
final ChildEntity child = s.find( ChildEntity.class, 1L );
final SimpleEntity parent = child.getParent();
t.rollback();
session.close();
// assert we have an uninitialized proxy
assertTrue( parent instanceof HibernateProxy );
assertFalse( Hibernate.isInitialized( parent ) );
assertEquals( "TheParent", parent.getName() );
// assert we have an initialized proxy now
assertTrue( Hibernate.isInitialized( parent ) );
}
finally {
if ( t.isActive() ) {
t.rollback();
}
s.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void testInvalidQueryMarksTxnForRollback() {
inSession(
session -> {
try {
assertFalse( session.getTransaction().isActive() );
session.getTransaction().begin();
// Query
session.createQuery( "invalid" ).list();
fail( "expecting failure" );
}
catch (IllegalArgumentException expected) {
assertTrue( session.getTransaction().isActive() );
assertTrue( session.getTransaction().getRollbackOnly() );
}
}
);
}
}
代码示例来源:origin: hibernate/hibernate-orm
session -> {
final Transaction transaction = session.getTransaction();
assertFalse( transaction.isActive() );
try {
transaction.getRollbackOnly();
代码示例来源:origin: hibernate/hibernate-orm
session -> {
final Transaction transaction = session.getTransaction();
assertFalse( transaction.isActive() );
try {
transaction.setRollbackOnly();
内容来源于网络,如有侵权,请联系作者删除!