use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class TransactionsTest method jdbc.
@Test
public void jdbc() {
//tag::transactions-api-jdbc-example[]
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc").build();
Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
Session session = sessionFactory.openSession();
try {
// calls Connection#setAutoCommit( false ) to
// signal start of transaction
session.getTransaction().begin();
session.createQuery("UPDATE customer set NAME = 'Sir. '||NAME").executeUpdate();
// calls Connection#commit(), if an error
// happens we attempt a rollback
session.getTransaction().commit();
} catch (Exception e) {
// where the exception happened
if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
session.getTransaction().rollback();
}
// handle the underlying error
} finally {
session.close();
sessionFactory.close();
}
//end::transactions-api-jdbc-example[]
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class ManagedSessionContext method unbind.
/**
* Unbinds the session (if one) current associated with the context for the
* given session.
*
* @param factory The factory for which to unbind the current session.
* @return The bound session if one, else null.
*/
public static Session unbind(SessionFactory factory) {
final Map<SessionFactory, Session> sessionMap = sessionMap();
Session existing = null;
if (sessionMap != null) {
existing = sessionMap.remove(factory);
doCleanup();
}
return existing;
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class SessionFactoryRegistry method clearRegistrations.
public void clearRegistrations() {
nameUuidXref.clear();
for (SessionFactory factory : sessionFactoryMap.values()) {
try {
factory.close();
} catch (Exception ignore) {
}
}
sessionFactoryMap.clear();
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class EntityManagerFactoryUnwrapTest method testEntityManagerCanBeUnwrappedToSessionFactory.
@Test
public void testEntityManagerCanBeUnwrappedToSessionFactory() {
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
assertNotNull("Unwrapping to API class SessionFactory should be ok", sessionFactory);
}
use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.
the class ConfigurationTest method testIgnoringHbm.
@Test
public void testIgnoringHbm() throws Exception {
Configuration cfg = new Configuration();
cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
cfg.setProperty(Configuration.ARTEFACT_PROCESSING_ORDER, "class");
SessionFactory sf = cfg.buildSessionFactory();
assertNotNull(sf);
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Query q;
try {
s.createQuery("from Boat").list();
fail("Boat should not be mapped");
} catch (IllegalArgumentException e) {
assertTyping(QuerySyntaxException.class, e.getCause());
//all good
}
q = s.createQuery("from Plane");
assertEquals(0, q.list().size());
tx.commit();
s.close();
sf.close();
}
Aggregations