use of org.jooq.tools.jdbc.LoggingConnection in project jOOQ by jOOQ.
the class Setup method run.
// This class sets up an EntityManager and configures the jOOQ DSLContext
// ----------------------------------------------------------------------
static void run(BiConsumer<EntityManager, DSLContext> consumer) throws Exception {
Connection connection = null;
EntityManagerFactory emf = null;
EntityManager em = null;
try {
// Bootstrapping JDBC:
Class.forName("org.h2.Driver");
connection = new LoggingConnection(DriverManager.getConnection("jdbc:h2:mem:jooq-jpa-example", "sa", ""));
final Connection c = connection;
// Creating an in-memory H2 database from our entities
MetadataSources metadata = new MetadataSources(new StandardServiceRegistryBuilder().applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect").applySetting("javax.persistence.schema-generation-connection", connection).applySetting("javax.persistence.create-database-schemas", true).applySetting(AvailableSettings.CONNECTION_PROVIDER, new ConnectionProvider() {
@SuppressWarnings("rawtypes")
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}
@Override
public Connection getConnection() {
return c;
}
@Override
public void closeConnection(Connection conn) throws SQLException {
}
@Override
public boolean supportsAggressiveRelease() {
return true;
}
}).build());
metadata.addAnnotatedClass(Actor.class);
metadata.addAnnotatedClass(Film.class);
metadata.addAnnotatedClass(Language.class);
SchemaExport export = new SchemaExport();
export.create(EnumSet.of(TargetType.DATABASE), metadata.buildMetadata());
Map<Object, Object> props = new HashMap<>();
DataSource ds = new SingleConnectionDataSource(connection);
props.put("hibernate.connection.datasource", ds);
props.put("hibernate.archive.autodetection", "");
emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(pui(ds), props);
em = emf.createEntityManager();
final EntityManager e = em;
// Run some Hibernate / jOOQ logic inside of a transaction
em.getTransaction().begin();
data(em);
consumer.accept(em, new DefaultConfiguration().set(connection).set(onStart(ctx -> e.flush())).dsl());
em.getTransaction().commit();
} finally {
if (em != null)
em.close();
if (emf != null)
emf.close();
if (connection != null)
connection.close();
}
}
Aggregations