• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java Transaction类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中net.sf.hibernate.Transaction的典型用法代码示例。如果您正苦于以下问题:Java Transaction类的具体用法?Java Transaction怎么用?Java Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



Transaction类属于net.sf.hibernate包,在下文中一共展示了Transaction类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: connect

import net.sf.hibernate.Transaction; //导入依赖的package包/类
private void connect() throws HibernateException {
	if (!isCurrentTransaction) {
		//if there is no current transaction callback registered
		//when we obtain the connection, try to register one now
		//note that this is not going to handle the case of
		//multiple-transactions-per-connection when the user is
		//manipulating transactions (need to use Hibernate txn)
		TransactionManager tm = factory.getTransactionManager();
		if (tm!=null) {
			try {
				javax.transaction.Transaction tx = tm.getTransaction();
				if ( isJTATransactionActive(tx) ) {
					tx.registerSynchronization( new CacheSynchronization(this) );
					isCurrentTransaction = true;
				}
			}
			catch (Exception e) {
				throw new TransactionException("could not register synchronization with JTA TransactionManager", e);
			}
		}
	}
	connection = batcher.openConnection();
	connect = false;
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:25,代码来源:SessionImpl.java


示例2: changeUserDetails

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates detached object support
 */
public void changeUserDetails(User user) throws Exception {
	System.out.println("Changing user details for: " + user.getId() );
	
	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
		
		s.saveOrUpdate(user);
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:24,代码来源:Main.java


示例3: changeItemDescription

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates automatic dirty checking
 */
public void changeItemDescription(Long itemId, String description) throws Exception {
	System.out.println("Changing auction item description for: " + itemId );
	
	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
	
		AuctionItem item = (AuctionItem) s.get(AuctionItem.class, itemId);
		if (item==null) throw new IllegalArgumentException("No item for the given id: " + itemId);
		item.setDescription(description);
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:26,代码来源:Main.java


示例4: hibernateInit

import net.sf.hibernate.Transaction; //导入依赖的package包/类
@Ignore
@Test
public void hibernateInit() throws Exception
{
    Database.init();

    Configuration config = new Configuration();
    config.configure("hibernate.cfg.xml");
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();

    // Filter-time code (H3SessionAjaxFilter)
    transaction.commit();

    // Shutdown code, when do we need to do this?
    sessionFactory.close();
}
 
开发者ID:directwebremoting,项目名称:dwr,代码行数:19,代码来源:HibernateConverterTest.java


示例5: createObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object createObject(Object obj){
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.save(obj);
	t.commit();
} catch (Exception ex) {
	ex.printStackTrace();
	try {
		t.rollback();
	} catch (Exception ex3){
		ex3.printStackTrace();
	}
	
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		ex2.printStackTrace();
	}
} 

 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:27,代码来源:HibernateDAO.java


示例6: viewAllAuctionsFast

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates HQL projection/aggregation
 */
public void viewAllAuctionsFast() throws Exception {
	System.out.println("Viewing all auction item info");

	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
	
		List auctions = s.createQuery(
			"select new AuctionInfo( item.id, item.description, item.ends, max(bid.amount) ) "
			+ "from AuctionItem item "
			+ "left join item.bids bid " 
			+ "group by item.id, item.description, item.ends "
			+ "order by item.ends desc"
			)
			.setMaxResults(100)
			.list();
			
		Iterator iter = auctions.iterator();
		while ( iter.hasNext() ) {
			AuctionInfo ai = (AuctionInfo) iter.next();
			System.out.println(
				"Auction: " + ai.getId() + " - " + ai.getDescription() + 
				", ends: " + ai.getEnds() + 
				", highest bid: " + ai.getMaxAmount() 
			);
		}
		System.out.println();
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:43,代码来源:Main.java


示例7: viewAllAuctionsSlow

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates HQL with runtime fetch strategy
 */
public void viewAllAuctionsSlow() throws Exception {
	System.out.println("Viewing all auction item objects");

	Session s = factory.openSession();
	Transaction tx=null;
	try {
		s.setFlushMode(FlushMode.NEVER); //entirely optional!!
		tx = s.beginTransaction();
	
		List auctions = s.createQuery(
			"from AuctionItem item "
			+ "left join fetch item.bids bid left join fetch bid.bidder " 
			+ "order by item.ends desc"
			)
			.setMaxResults(100)
			.list();
			
		Iterator iter = new HashSet(auctions).iterator();
		while ( iter.hasNext() ) {
			AuctionItem auction = (AuctionItem) iter.next();
			System.out.println( 
				"Auction: " + auction.getId() + " - " + auction.getDescription() + 
				", ends: " + auction.getEnds() + 
				", bids: " + auction.getBids() 
			);
		}
		System.out.println();
	
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:42,代码来源:Main.java


示例8: bidOnAuction

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates transitive persistence with detached object support
 */
public void bidOnAuction(User bidder, AuctionItem item, float amount) throws Exception {
	System.out.println("Creating a new bid for auction item: " + item.getId() + " by user: " + bidder.getId() );
	
	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
	
		s.lock(item, LockMode.NONE);
		s.lock(bidder, LockMode.NONE);
			
		Bid bid = new Bid();
		bid.setBidder(bidder);
		bid.setDatetime( new Date() );
		bid.setAmount(amount);
		bid.setItem(item);
		bidder.getBids().add(bid);
		item.getBids().add(bid);
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:33,代码来源:Main.java


示例9: viewUserAuctions

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates query by criteria with runtime fetch strategy
 */
public void viewUserAuctions(Long sellerId) throws Exception {
	System.out.println("Viewing user and auctions: " + sellerId);
	
	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
	
		List list = s.createCriteria(User.class)
			.add( Expression.eq("id", sellerId) )
			.setFetchMode("auctions", FetchMode.EAGER)
			.list();
		
		if (list.size()==0) throw new IllegalArgumentException("No user for the given id: " + sellerId);
		User user = (User) list.get(0);
		System.out.println(
			"User: " + user.getId() + " - " + user.getName() + 
			", email: " + user.getEmail() +
			", auctions: " + user.getAuctions()
		);
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:35,代码来源:Main.java


示例10: viewAuctionsByDescription

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Demonstrates query by example
 */
public void viewAuctionsByDescription(String description, int condition) throws Exception {
	String msg = "Viewing auctions containing: " + description;
	if (condition>0) msg += " with condition: " + condition + "/10";

	AuctionItem item = new AuctionItem();
	item.setDescription(description);
	item.setCondition(condition);
	
	Session s = factory.openSession();
	Transaction tx=null;
	try {
		tx = s.beginTransaction();
	
		Iterator iter = s.createCriteria(AuctionItem.class)
			.add( Example.create(item)
				.enableLike(MatchMode.ANYWHERE)
				.ignoreCase()
				.excludeZeroes() 
			)
			.list()
			.iterator();
		
		System.out.println(msg);
		while ( iter.hasNext() ) {
			item = (AuctionItem) iter.next();
			System.out.println("Item: " + item.getId() + " - " + item.getDescription() );
		}
		System.out.println();
		
		tx.commit();
	}
	catch (Exception e) {
		if (tx!=null) tx.rollback();
		throw e;
	}
	finally {
		s.close();
	}
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:43,代码来源:Main.java


示例11: rollbackHard

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Rolls back transaction ignoring exceptions
 *
 * @param transaction
 */
public static void rollbackHard(final Transaction transaction) {
  try {
    transaction.rollback();
  } catch (Exception e) {
    // ignore
    log.warn("Exception while rolling back a TX", e);
  }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:14,代码来源:PersistanceUtils.java


示例12: test_SaveNewSystemProperty

import net.sf.hibernate.Transaction; //导入依赖的package包/类
/**
 * Tests saving modified property
 */
public void test_SaveNewSystemProperty() throws Exception {
  final SystemProperty systemProperty = new SystemProperty();
  systemProperty.setPropertyName("test.property.name");
  systemProperty.setPropertyValue("test.property.value");
  final Session session = configManager.openSession();
  final Transaction transacton = session.beginTransaction();
  session.saveOrUpdate(systemProperty);
  transacton.commit();

  session.close();
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:15,代码来源:SSTestConfigurationManager.java


示例13: main

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public static void main(String[] args) throws HibernateException
{

    Person person = new Person();
    person.setName("John Smith");
    person.setEmail("[email protected]");

    Session session = _sessions.openSession();

    Transaction tx = null;
    try
    {
        tx = session.beginTransaction();
        session.save(person);
        tx.commit();
    }
    catch (HibernateException he)
    {
        if (tx != null)
            tx.rollback();
        throw he;
    }
    finally
    {
        session.close();
    }

}
 
开发者ID:windup,项目名称:windup-rulesets,代码行数:29,代码来源:Example.java


示例14: createObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object createObject(Object obj) throws ApplicationException{
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.save(obj);
	t.commit();
} catch (Exception ex) {
	log.error(ex);
	try {
		t.rollback();
	} catch (Exception ex3){
		if (log.isDebugEnabled())
			log.debug("createObject|Failure|Error in Rolling Back Transaction|"+ ex3.getMessage());
	}
	if (log.isDebugEnabled())
		log	.debug("createObject|Failure|Error in creating the "+ obj.getClass().getName()+ ex.getMessage());
	
	throw new ApplicationException("An error occured in creating the "+obj.getClass().getName() + "\n" + ex.getMessage());
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		if (log.isDebugEnabled())
			log.debug("createObject|Failure|Error in Closing Session |"	+ ex2.getMessage());
	}
} 
if (log.isDebugEnabled())
	log	.debug("createObject|Success|Successful in creating the "+ obj.getClass().getName());
 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:33,代码来源:HibernateDAO.java


示例15: updateObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object updateObject(Object obj) throws ApplicationException{
 	
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.update(obj);
	t.commit();
} catch (Exception ex) {
	log.error(ex);
	try {
		t.rollback();
	} catch (Exception ex3){
		if (log.isDebugEnabled())
			log.debug("updateObject|Failure|Error in Rolling Back Transaction|"+ ex3.getMessage());
	}
	if (log.isDebugEnabled())
		log	.debug("updateObject|Failure|Error in updating the "+ obj.getClass().getName()+ ex.getMessage());
	
	throw new ApplicationException("An error occured in updating the "+obj.getClass().getName() + "\n" + ex.getMessage());
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		if (log.isDebugEnabled())
			log.debug("updateObject|Failure|Error in Closing Session |"	+ ex2.getMessage());
	}
} 
if (log.isDebugEnabled())
	log	.debug("updateObject|Success|Successful in updating the "+ obj.getClass().getName());
 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:34,代码来源:HibernateDAO.java


示例16: removeObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public void removeObject(Object obj) throws ApplicationException{
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.delete(obj);
	t.commit();
} catch (Exception ex) {
	log.error(ex);
	try {
		t.rollback();
	} catch (Exception ex3){
		if (log.isDebugEnabled())
			log.debug("deleteObject|Failure|Error in Rolling Back Transaction|"+ ex3.getMessage());
	}
	if (log.isDebugEnabled())
		log	.debug("deleteObject|Failure|Error in removing the "+ obj.getClass().getName()+ ex.getMessage());
	
	throw new ApplicationException("An error occured in removing the "+obj.getClass().getName() + "\n" + ex.getMessage());
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		if (log.isDebugEnabled())
			log.debug("deleteObject|Failure|Error in Closing Session |"	+ ex2.getMessage());
	}
} 
if (log.isDebugEnabled())
	log	.debug("deleteObject|Success|Successful in deleting the "+ obj.getClass().getName());
 	
 	
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:34,代码来源:HibernateDAO.java


示例17: updateObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object updateObject(Object obj) {
 	
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.update(obj);
	t.commit();
} catch (Exception ex) {
	ex.printStackTrace();
	try {
		t.rollback();
	} catch (Exception ex3){
		ex3.printStackTrace();
	}
	
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		ex2.printStackTrace();
	}
} 

 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:28,代码来源:HibernateDAO.java


示例18: removeObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public void removeObject(Object obj) {
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.delete(obj);
	t.commit();
} catch (Exception ex) {
	ex.printStackTrace();
	try {
		t.rollback();
	} catch (Exception ex3){
		ex3.printStackTrace();
		}
	
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		ex2.printStackTrace();
	}
} 

 	
 	
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:28,代码来源:HibernateDAO.java


示例19: createObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object createObject(Object obj) throws ApplicationException{
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.save(obj);
	t.commit();
} catch (Exception ex) {
	log.error(ex);
	try {
		t.rollback();
	} catch (Exception ex3){
		if (log.isDebugEnabled())
			log.debug("createObject|Failure|Error in Rolling Back Transaction|"+ ex3.getMessage());
	}
	if (log.isDebugEnabled())
		log	.debug("createObject|Failure|Error in creating the "+ obj.getClass().getName()+ ex.getMessage());
	
	throw new ApplicationException("An error occured in creating the "+obj.getClass().getName() + "\n" + ex.getMessage(), ex);
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		if (log.isDebugEnabled())
			log.debug("createObject|Failure|Error in Closing Session |"	+ ex2.getMessage());
	}
} 
if (log.isDebugEnabled())
	log	.debug("createObject|Success|Successful in creating the "+ obj.getClass().getName());
 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:33,代码来源:HibernateDAO.java


示例20: updateObject

import net.sf.hibernate.Transaction; //导入依赖的package包/类
public Object updateObject(Object obj) throws ApplicationException{
 	
 	Session s = null;
Transaction t = null;
try {
	s = sf.openSession();
	t = s.beginTransaction();
	s.update(obj);
	t.commit();
} catch (Exception ex) {
	log.error(ex);
	try {
		t.rollback();
	} catch (Exception ex3){
		if (log.isDebugEnabled())
			log.debug("updateObject|Failure|Error in Rolling Back Transaction|"+ ex3.getMessage());
	}
	if (log.isDebugEnabled())
		log	.debug("updateObject|Failure|Error in updating the "+ obj.getClass().getName()+ ex.getMessage());
	
	throw new ApplicationException("An error occured in updating the "+obj.getClass().getName() + "\n" + ex.getMessage(), ex);
}  finally {
	try {
		s.close();
	} catch (Exception ex2) {
		if (log.isDebugEnabled())
			log.debug("updateObject|Failure|Error in Closing Session |"	+ ex2.getMessage());
	}
} 
if (log.isDebugEnabled())
	log	.debug("updateObject|Success|Successful in updating the "+ obj.getClass().getName());
 	return obj;
 }
 
开发者ID:NCIP,项目名称:common-security-module,代码行数:34,代码来源:HibernateDAO.java



注:本文中的net.sf.hibernate.Transaction类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java Handler类代码示例发布时间:2022-05-23
下一篇:
Java Viewport类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap