Java tutorial
/** * * Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); You may not * use this file except in compliance with the License. You may obtain a copy of * the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.speedment.orm.examples.hares; import net.ttddyy.dsproxy.listener.SLF4JQueryLoggingListener; import net.ttddyy.dsproxy.support.ProxyDataSource; import org.hibernate.Interceptor; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hsqldb.jdbc.JDBCDataSource; import org.junit.After; import org.junit.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.sql.DataSource; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Properties; import java.util.concurrent.*; public abstract class AbstractTest { protected enum LockType { LOCKS, MVLOCKS, MVCC } static { Thread.currentThread().setName("Alice"); } private final ExecutorService executorService = Executors.newSingleThreadExecutor(r -> { Thread bob = new Thread(r); bob.setName("Bob"); return bob; }); protected final Logger LOGGER = LoggerFactory.getLogger(getClass()); @FunctionalInterface protected static interface VoidCallable extends Callable<Void> { void execute(); default Void call() throws Exception { execute(); return null; } } @FunctionalInterface protected static interface SessionCallable<T> { T execute(Session session); } @FunctionalInterface protected static interface SessionVoidCallable { void execute(Session session); } @FunctionalInterface protected static interface TransactionCallable<T> extends SessionCallable<T> { default void beforeTransactionCompletion() { } default void afterTransactionCompletion() { } } @FunctionalInterface protected static interface TransactionVoidCallable extends SessionVoidCallable { default void beforeTransactionCompletion() { } default void afterTransactionCompletion() { } } private SessionFactory sf; @Before public void init() { sf = newSessionFactory(); } @After public void destroy() { sf.close(); } public SessionFactory getSessionFactory() { return sf; } protected abstract Class<?>[] entities(); protected String[] packages() { return null; } protected Interceptor interceptor() { return null; } private SessionFactory newSessionFactory() { Properties properties = getProperties(); Configuration configuration = new Configuration().addProperties(properties); for (Class<?> entityClass : entities()) { configuration.addAnnotatedClass(entityClass); } String[] packages = packages(); if (packages != null) { for (String scannedPackage : packages) { configuration.addPackage(scannedPackage); } } Interceptor interceptor = interceptor(); if (interceptor != null) { configuration.setInterceptor(interceptor); } return configuration .buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(properties).build()); } protected Properties getProperties() { Properties properties = new Properties(); properties.put("hibernate.dialect", hibernateDialect()); //log settings properties.put("hibernate.hbm2ddl.auto", "create-drop"); //data source settings properties.put("hibernate.connection.datasource", newDataSource()); return properties; } private ProxyDataSource newDataSource() { ProxyDataSource proxyDataSource = new ProxyDataSource(); proxyDataSource.setDataSource(dataSource()); proxyDataSource.setListener(new SLF4JQueryLoggingListener()); return proxyDataSource; } protected String hibernateDialect() { return "org.hibernate.dialect.HSQLDialect"; } protected DataSource dataSource() { JDBCDataSource dataSource = new JDBCDataSource(); dataSource.setUrl("jdbc:hsqldb:mem:test;hsqldb.tx=" + lockType().name().toLowerCase()); dataSource.setUser("sa"); dataSource.setPassword(""); return dataSource; } protected <T> T doInTransaction(TransactionCallable<T> callable) { T result = null; Session session = null; Transaction txn = null; try { session = sf.openSession(); callable.beforeTransactionCompletion(); txn = session.beginTransaction(); result = callable.execute(session); txn.commit(); } catch (RuntimeException e) { if (txn != null && txn.isActive()) txn.rollback(); throw e; } finally { callable.afterTransactionCompletion(); if (session != null) { session.close(); } } return result; } protected void doInTransaction(TransactionVoidCallable callable) { Session session = null; Transaction txn = null; try { session = sf.openSession(); callable.beforeTransactionCompletion(); txn = session.beginTransaction(); callable.execute(session); txn.commit(); } catch (RuntimeException e) { if (txn != null && txn.isActive()) txn.rollback(); throw e; } finally { callable.afterTransactionCompletion(); if (session != null) { session.close(); } } } protected LockType lockType() { return LockType.LOCKS; } }