use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.
the class SuppliedConnectionTest method prepareTest.
@Override
protected void prepareTest() throws Exception {
super.prepareTest();
try {
Connection conn = cp.getConnection();
try {
final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry(), conn), true);
new SchemaCreatorImpl(serviceRegistry()).doCreation(metadata(), false, target);
} finally {
cp.closeConnection(conn);
}
} catch (Throwable ignore) {
}
}
use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.
the class DiscriminatorMultiTenancyTest method setUp.
@Before
public void setUp() {
Map<String, Object> settings = new HashMap<>();
settings.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantResolver);
settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
settings.put(Environment.GENERATE_STATISTICS, "true");
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
try {
MetadataSources ms = new MetadataSources(serviceRegistry);
ms.addAnnotatedClass(Customer.class);
Metadata metadata = ms.buildMetadata();
final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
customerMapping.setCached(true);
((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
tool.injectServices(serviceRegistry);
connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
final GenerationTargetToDatabase target = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, connectionProvider));
new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, target);
new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, target);
target.release();
final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
sessionFactory = (SessionFactoryImplementor) sfb.build();
currentTenantResolver.setHibernateBooted();
} catch (Throwable t) {
serviceRegistry.close();
throw t;
}
}
use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.
the class AbstractSchemaBasedMultiTenancyTest method setUp.
@Before
public void setUp() {
T multiTenantConnectionProvider = buildMultiTenantConnectionProvider();
Map<String, Object> settings = new HashMap<>();
settings.put(Environment.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName());
settings.put(Environment.GENERATE_STATISTICS, "true");
serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).addService(MultiTenantConnectionProvider.class, multiTenantConnectionProvider).build();
MetadataSources ms = new MetadataSources(serviceRegistry);
ms.addAnnotatedClass(Customer.class);
ms.addAnnotatedClass(Invoice.class);
Metadata metadata = ms.buildMetadata();
final PersistentClass customerMapping = metadata.getEntityBinding(Customer.class.getName());
customerMapping.setCached(true);
((RootClass) customerMapping).setCacheConcurrencyStrategy("read-write");
HibernateSchemaManagementTool tool = new HibernateSchemaManagementTool();
tool.injectServices(serviceRegistry);
final GenerationTargetToDatabase acmeTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, acmeProvider));
final GenerationTargetToDatabase jbossTarget = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, jbossProvider));
new SchemaDropperImpl(serviceRegistry).doDrop(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
new SchemaCreatorImpl(serviceRegistry).doCreation(metadata, serviceRegistry, settings, true, acmeTarget, jbossTarget);
final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
configure(sfb);
sessionFactory = (SessionFactoryImplementor) sfb.build();
}
use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.
the class PostgreSQLMultipleSchemaSequenceTest method test.
@Test
@TestForIssue(jiraKey = "HHH-5538")
public void test() {
StandardServiceRegistry ssr1 = new StandardServiceRegistryBuilder().build();
final String extraSchemaName = "extra_schema_sequence_validation";
try {
final MetadataImplementor metadata1 = (MetadataImplementor) new MetadataSources(ssr1).addAnnotatedClass(Box.class).buildMetadata();
try {
new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata1);
final ConnectionProvider connectionProvider1 = ssr1.getService(ConnectionProvider.class);
DdlTransactionIsolatorTestingImpl ddlTransactionIsolator1 = new DdlTransactionIsolatorTestingImpl(ssr1, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider1));
try (Statement statement = ddlTransactionIsolator1.getIsolatedConnection().createStatement()) {
statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
statement.execute(String.format("CREATE SCHEMA %s;", extraSchemaName));
try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
while (resultSet.next()) {
Long sequenceValue = resultSet.getLong(1);
assertEquals(Long.valueOf(1L), sequenceValue);
}
}
} catch (SQLException e) {
fail(e.getMessage());
}
String existingUrl = (String) Environment.getProperties().get(AvailableSettings.URL);
if (existingUrl.indexOf('?') == -1) {
existingUrl += "?";
} else {
existingUrl += "&";
}
StandardServiceRegistry ssr2 = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.URL, existingUrl + "currentSchema=" + extraSchemaName).build();
try {
final MetadataImplementor metadata2 = (MetadataImplementor) new MetadataSources(ssr2).addAnnotatedClass(Box.class).buildMetadata();
try {
new SchemaExport().setOutputFile(output.getAbsolutePath()).create(EnumSet.of(TargetType.DATABASE, TargetType.SCRIPT), metadata2);
} finally {
final ConnectionProvider connectionProvider2 = ssr2.getService(ConnectionProvider.class);
DdlTransactionIsolatorTestingImpl ddlTransactionIsolator2 = new DdlTransactionIsolatorTestingImpl(ssr2, new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess(connectionProvider2));
try (Statement statement = ddlTransactionIsolator2.getIsolatedConnection().createStatement()) {
try (ResultSet resultSet = statement.executeQuery("SELECT NEXTVAL('SEQ_TEST')")) {
while (resultSet.next()) {
Long sequenceValue = resultSet.getLong(1);
assertEquals(Long.valueOf(1L), sequenceValue);
}
}
statement.execute(String.format("DROP SCHEMA IF EXISTS %s CASCADE", extraSchemaName));
} catch (SQLException e) {
fail(e.getMessage());
}
new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata2);
}
} finally {
StandardServiceRegistryBuilder.destroy(ssr2);
}
} finally {
// clean up
new SchemaExport().drop(EnumSet.of(TargetType.DATABASE), metadata1);
}
final List<String> sqlLines = Files.readAllLines(output.toPath(), Charset.defaultCharset());
assertEquals(2, sqlLines.stream().filter(s -> s.equalsIgnoreCase("create sequence SEQ_TEST start with 1 increment by 1;")).count());
} catch (IOException e) {
fail(e.getMessage());
} finally {
StandardServiceRegistryBuilder.destroy(ssr1);
}
}
use of org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl in project hibernate-orm by hibernate.
the class IndividuallySchemaValidatorImplTest method testMissingColumn.
@Test
public void testMissingColumn() {
MetadataSources metadataSources = new MetadataSources(ssr);
metadataSources.addAnnotatedClass(NoNameColumn.class);
MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();
Map<String, Object> settings = new HashMap<>();
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) new StandardServiceRegistryBuilder().applySettings(settings).build();
DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
connectionProvider.configure(PropertiesHelper.map(properties()));
final GenerationTargetToDatabase schemaGenerator = new GenerationTargetToDatabase(new DdlTransactionIsolatorTestingImpl(serviceRegistry, new JdbcConnectionAccessImpl(connectionProvider)));
try {
new SchemaCreatorImpl(ssr).doCreation(metadata, serviceRegistry, settings, true, schemaGenerator);
metadataSources = new MetadataSources(ssr);
metadataSources.addAnnotatedClass(NameColumn.class);
metadata = (MetadataImplementor) metadataSources.buildMetadata();
metadata.validate();
try {
getSchemaValidator(metadata);
Assert.fail("SchemaManagementException expected");
} catch (SchemaManagementException e) {
assertEquals("Schema-validation: missing column [name] in table [SomeSchema.ColumnEntity]", e.getMessage());
}
} finally {
new SchemaDropperImpl(serviceRegistry).doDrop(metadata, false, schemaGenerator);
serviceRegistry.destroy();
connectionProvider.stop();
}
}
Aggregations