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();
}
}
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();
});
}
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);
}
}
}
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();
}
}
}
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();
}
}
}
Aggregations