Java Code Examples for org.hibernate.Transaction#begin()

The following examples show how to use org.hibernate.Transaction#begin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSharedSessionContract.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Transaction beginTransaction() {
	checkOpen();

	Transaction result = getTransaction();
	result.begin();

	return result;
}
 
Example 2
Source File: SessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Transaction beginTransaction() throws HibernateException {
	errorIfClosed();
	if ( rootSession != null ) {
		// todo : should seriously consider not allowing a txn to begin from a child session
		//      can always route the request to the root session...
		log.warn( "Transaction started on non-root session" );
	}
	Transaction result = getTransaction();
	result.begin();
	return result;
}
 
Example 3
Source File: FooFixtures.java    From tutorials with MIT License 5 votes vote down vote up
public void createBars() {
    Session session = null;
    Transaction tx = null;
    session = sessionFactory.openSession();
    tx = session.getTransaction();
    try {
        tx.begin();
        for (int i = 156; i < 160; i++) {
            final Bar bar = new Bar();
            bar.setName("Bar_" + i);
            final Foo foo = new Foo("Foo_" + (i + 120));
            foo.setBar(bar);
            session.save(foo);
            final Foo foo2 = new Foo(null);
            if (i % 2 == 0)
                foo2.setName("LuckyFoo" + (i + 120));
            foo2.setBar(bar);
            session.save(foo2);
            bar.getFooSet().add(foo);
            bar.getFooSet().add(foo2);
            session.merge(bar);
        }
        tx.commit();
        session.flush();
    } catch (final HibernateException he) {
        if (tx != null)
            tx.rollback();
        System.out.println("Not able to open session");
        he.printStackTrace();
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null)
            session.close();
    }

}
 
Example 4
Source File: DataSourceDAOHibImpl.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Insert data source.
 *
 * @param aDataSource
 *            the a data source
 * @throws EMFUserError
 *             the EMF user error
 * @see it.eng.spagobi.tools.datasource.dao.IDataSourceDAO#insertDataSource(it.eng.spagobi.tools.datasource.bo.IDataSource)
 */
@Override
public Integer insertDataSource(IDataSource aDataSource, String organization) throws EMFUserError {
	logger.debug("IN");
	Session aSession = null;
	Transaction tx = null;
	Integer id = null;
	try {
		aSession = getSession();
		tx = aSession.beginTransaction();

		Criterion aCriterion = Expression.and(Expression.eq("domainCd", "DIALECT_HIB"), Expression.eq("valueCd", aDataSource.getDialectName()));
		Criteria criteria = aSession.createCriteria(SbiDomains.class);
		criteria.add(aCriterion);

		SbiDomains dialect = (SbiDomains) criteria.uniqueResult();

		if (dialect == null) {
			logger.error("The Domain with value_cd=" + aDataSource.getDialectName() + " does not exist.");
			throw new EMFUserError(EMFErrorSeverity.ERROR, 1035);
		}

		SbiDataSource hibDataSource = toSbiDataSource(aDataSource);
		hibDataSource.setDialect(dialect);
		hibDataSource.setDialectDescr(dialect.getValueNm());
		hibDataSource.setReadOnly(aDataSource.checkIsReadOnly());

		disableOtherWriteDefault(aDataSource, hibDataSource, aSession);

		hibDataSource.setWriteDefault(aDataSource.checkIsWriteDefault());

		hibDataSource.getCommonInfo().setOrganization(organization);

		updateSbiCommonInfo4Insert(hibDataSource);
		id = (Integer) aSession.save(hibDataSource);
		tx.commit();
		aSession.flush();
		tx.begin();
		SbiTenant sbiOrganizations = DAOFactory.getTenantsDAO().loadTenantByName(hibDataSource.getCommonInfo().getOrganization());

		SbiOrganizationDatasource sbiOrganizationDatasource = new SbiOrganizationDatasource();
		sbiOrganizationDatasource.setSbiDataSource(hibDataSource);
		sbiOrganizationDatasource.setSbiOrganizations(sbiOrganizations);
		SbiOrganizationDatasourceId idRel = new SbiOrganizationDatasourceId();
		idRel.setDatasourceId(id);
		idRel.setOrganizationId(sbiOrganizations.getId());
		sbiOrganizationDatasource.setId(idRel);

		sbiOrganizationDatasource.getCommonInfo().setOrganization(organization);

		updateSbiCommonInfo4Insert(sbiOrganizationDatasource);

		aSession.save(sbiOrganizationDatasource);

		tx.commit();
	} catch (HibernateException he) {
		logger.error("Error while inserting the data source with id " + ((aDataSource == null) ? "" : String.valueOf(aDataSource.getDsId())), he);

		if (tx != null)
			tx.rollback();

		throw new EMFUserError(EMFErrorSeverity.ERROR, 100);

	} finally {
		if (aSession != null) {
			if (aSession.isOpen())
				aSession.close();
			logger.debug("OUT");
		}

	}
	return id;
}
 
Example 5
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Transaction beginTransaction() throws HibernateException {
	errorIfClosed();
	Transaction result = getTransaction();
	result.begin();
	return result;
}
 
Example 6
Source File: HibernateLifecycleUtil.java    From tutorials with MIT License 4 votes vote down vote up
public static Transaction startTransaction(Session s) {
    Transaction tx = s.getTransaction();
    tx.begin();
    return tx;
}
 
Example 7
Source File: Application.java    From coditori with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

        Session s = HibernateConfig.getSession();
        Transaction tx= s.getTransaction();

        User user = new User();
        user.setName("Masoud");

        tx.begin();

        s.save(user);

        tx.commit();
        s.close();
    }