@Override public ConnectionProvider buildConnectionProvider() { if (this.dataSourceProvider == null) { throw new IllegalStateException(); } LOG.debug("Building new Connection Provider using datasourceProvider"); DatasourceConnectionProviderImpl connectionProvider = new DatasourceConnectionProviderImpl(); connectionProvider.setDataSource(dataSourceProvider.getDataSource()); connectionProvider.configure(Collections.emptyMap()); return connectionProvider; }
@Override public synchronized SessionFactory buildSessionFactory() { try { LOG.debug("Building new session factory"); SessionFactory real = configurationProvider.getConfiguration().buildSessionFactory( this.serviceRegistryBuilder.buildServiceRegistry()); LOG.debug("Session factory created, creating new Transaction Manager to handle transactions"); HibernateTransactionManager txManager = new HibernateTransactionManager(); txManager.setDataSource(((DatasourceConnectionProviderImpl) ((SessionFactoryImplementor) real).getServiceRegistry() .getService(ConnectionProvider.class)).getDataSource()); txManager.setSessionFactory(proxySessionFactory); txManager.setValidateExistingTransaction(true); txManager.afterPropertiesSet(); // The anotations at this point uses the new transaction manager AnnotationTransactionAspect txAspect = AnnotationTransactionAspect.aspectOf(); txAspect.setTransactionManager(txManager); LOG.debug("New transaction manager established to AnnotationTransactionAspect, now the annotations used the new Transaction Manager"); return real; } catch (RuntimeException ex) { LOG.error("Runtime exception when trying to create session factory.", ex); throw ex; } }
/** * Get Default DataSource. JNDI name of datasource is taken from Entity Manager * * @return * @throws NamingException */ public static DataSource getDefaultDataSource(EntityManager em) throws NamingException { // Doesn't work because of https://hibernate.atlassian.net/browse/HHH-8121 // EntityManagerFactory emFactory = em.getEntityManagerFactory(); // Object ds = emFactory.getProperties().get("javax.persistence.jtaDataSource"); // InitialContext initialContext = new InitialContext(); // Object lookup = initialContext.lookup(ds.toString()); // return (DataSource) lookup; // TODO: Find better way how to get DataSource from JPA without using hard coded JNDI name or casting to JPA implementation SessionFactoryImpl factory = (SessionFactoryImpl) em.unwrap(Session.class).getSessionFactory(); // or directly cast the sessionFactory DatasourceConnectionProviderImpl provider = (DatasourceConnectionProviderImpl) factory.getConnectionProvider(); return provider.getDataSource(); }
/** {@inheritDoc} */ @Nullable @Override protected ServiceRegistryBuilder registryBuilder() { ServiceRegistryBuilder builder = new ServiceRegistryBuilder(); DatasourceConnectionProviderImpl connProvider = new DatasourceConnectionProviderImpl(); BasicManagedDataSource dataSrc = new BasicManagedDataSource(); // JTA-aware data source. dataSrc.setTransactionManager(jotm.getTransactionManager()); dataSrc.setDefaultAutoCommit(false); JdbcDataSource h2DataSrc = new JdbcDataSource(); h2DataSrc.setURL(CONNECTION_URL); dataSrc.setXaDataSourceInstance(h2DataSrc); connProvider.setDataSource(dataSrc); connProvider.configure(Collections.emptyMap()); builder.addService(ConnectionProvider.class, connProvider); builder.addService(JtaPlatform.class, new TestJtaPlatform()); builder.addService(TransactionFactory.class, new JtaTransactionFactory()); return builder; }