Search in sources :

Example 6 with Session

use of org.hibernate.Session in project dropwizard by dropwizard.

the class UnitOfWorkAwareProxyFactoryTest method setUp.

@Before
public void setUp() throws Exception {
    final HibernateBundle<?> bundle = mock(HibernateBundle.class);
    final Environment environment = mock(Environment.class);
    when(environment.lifecycle()).thenReturn(mock(LifecycleEnvironment.class));
    when(environment.metrics()).thenReturn(new MetricRegistry());
    final DataSourceFactory dataSourceFactory = new DataSourceFactory();
    dataSourceFactory.setUrl("jdbc:hsqldb:mem:unit-of-work-" + UUID.randomUUID().toString());
    dataSourceFactory.setUser("sa");
    dataSourceFactory.setDriverClass("org.hsqldb.jdbcDriver");
    dataSourceFactory.setValidationQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
    dataSourceFactory.setProperties(ImmutableMap.of("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"));
    dataSourceFactory.setInitialSize(1);
    dataSourceFactory.setMinSize(1);
    sessionFactory = new SessionFactoryFactory().build(bundle, environment, dataSourceFactory, ImmutableList.of());
    try (Session session = sessionFactory.openSession()) {
        Transaction transaction = session.beginTransaction();
        session.createNativeQuery("create table user_sessions (token varchar(64) primary key, username varchar(16))").executeUpdate();
        session.createNativeQuery("insert into user_sessions values ('67ab89d', 'jeff_28')").executeUpdate();
        transaction.commit();
    }
}
Also used : DataSourceFactory(io.dropwizard.db.DataSourceFactory) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Transaction(org.hibernate.Transaction) MetricRegistry(com.codahale.metrics.MetricRegistry) Environment(io.dropwizard.setup.Environment) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) Session(org.hibernate.Session) Before(org.junit.Before)

Example 7 with Session

use of org.hibernate.Session in project dropwizard by dropwizard.

the class SessionFactoryHealthCheck method check.

@Override
protected Result check() throws Exception {
    return timeBoundHealthCheck.check(() -> {
        try (Session session = sessionFactory.openSession()) {
            final Transaction txn = session.beginTransaction();
            try {
                session.createNativeQuery(validationQuery).list();
                txn.commit();
            } catch (Exception e) {
                if (txn.getStatus().canRollback()) {
                    txn.rollback();
                }
                throw e;
            }
        }
        return Result.healthy();
    });
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 8 with Session

use of org.hibernate.Session in project onebusaway-gtfs-modules by OneBusAway.

the class HibernateOperationsImpl method execute.

@Override
public Object execute(HibernateOperation callback) {
    if (_session == null) {
        Session session = _sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        tx.begin();
        try {
            Object result = callback.doInHibernate(session);
            tx.commit();
            return result;
        } catch (Exception ex) {
            tx.rollback();
            throw new IllegalStateException(ex);
        } finally {
            session.close();
        }
    } else {
        try {
            return callback.doInHibernate(_session);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) SQLException(java.sql.SQLException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 9 with Session

use of org.hibernate.Session in project druid by alibaba.

the class HibernateCRUDTest method test_transactional_update.

public void test_transactional_update() {
    Session session = null;
    Transaction tran = null;
    try {
        session = sessionFactory.openSession();
        tran = session.beginTransaction();
        doCreate(session);
        doUpdate(session);
    } finally {
        if (tran != null) {
            tran.commit();
        }
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Example 10 with Session

use of org.hibernate.Session in project druid by alibaba.

the class HibernateCRUDTest method test_transactional_create.

public void test_transactional_create() {
    Session session = null;
    Transaction tran = null;
    try {
        session = sessionFactory.openSession();
        tran = session.beginTransaction();
        doCreate(session);
    } finally {
        if (tran != null) {
            tran.commit();
        }
        if (session != null) {
            session.flush();
            session.close();
        }
    }
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session)

Aggregations

Session (org.hibernate.Session)3262 Test (org.junit.Test)2414 Transaction (org.hibernate.Transaction)1255 List (java.util.List)526 ArrayList (java.util.ArrayList)363 TestForIssue (org.hibernate.testing.TestForIssue)360 Query (org.hibernate.Query)224 Test (org.testng.annotations.Test)143 Iterator (java.util.Iterator)127 Date (java.util.Date)112 Map (java.util.Map)104 BigDecimal (java.math.BigDecimal)97 Criteria (org.hibernate.Criteria)92 HashSet (java.util.HashSet)90 HibernateException (org.hibernate.HibernateException)82 HashMap (java.util.HashMap)70 SQLException (java.sql.SQLException)66 SkipForDialect (org.hibernate.testing.SkipForDialect)66 Connection (java.sql.Connection)65 Serializable (java.io.Serializable)62