本文整理汇总了Java中org.hibernate.StatelessSession.close方法的典型用法代码示例。如果您正苦于以下问题:Java StatelessSession.close方法的具体用法?Java StatelessSession.close怎么用?Java StatelessSession.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.StatelessSession
的用法示例。
在下文中一共展示了StatelessSession.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runStatelessHql
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private static void runStatelessHql() throws Exception {
Stopwatch watch = Stopwatch.createStarted();
StatelessSession statelessSession = sessionFactory.openStatelessSession();
try {
statelessSession.getTransaction().begin();
Query query = statelessSession
.createQuery(
" SELECT d.id, d.firstName, d.lastName, c.id, c.make " + " FROM Driver d "
+ " LEFT JOIN d.cars c WHERE index(c) LIKE 'Good'").setFetchSize(0).setReadOnly(true);
ScrollableResults scroll = query.scroll(ScrollMode.FORWARD_ONLY);
while (scroll.next()) {
LOG.info("Entry " + scroll.get(0));
}
statelessSession.getTransaction().commit();
} catch (Exception ex) {
statelessSession.getTransaction().rollback();
throw ex;
} finally {
statelessSession.close();
}
LOG.info("StatelessHql:=" + watch.toString());
}
示例2: removeLocalization
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private void removeLocalization(EntityPersister persister, Object entity) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
List<LocalizedObjectTypes> objType = obj.getLocalizedObjectTypes();
if (objType.size() > 0) {
long key = obj.getKey();
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
org.hibernate.Query query = session.createQuery(
"DELETE FROM LocalizedResource WHERE objectKey = :objectKey AND objectType IN (:objectType)");
query.setParameter("objectKey", key);
query.setParameterList("objectType", objType);
query.executeUpdate();
tx.commit();
session.close();
}
}
}
示例3: createHistory
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private void createHistory(EntityPersister persister, Object entity,
ModificationType type) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
if (obj.hasHistory()) {
final DomainHistoryObject<?> hist = HistoryObjectFactory.create(
obj, type, DataServiceBean.getCurrentHistoryUser());
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
session.insert(hist);
tx.commit();
session.close();
if (logger.isDebugLoggingEnabled()) {
logger.logDebug(String.format("%s %s[%s, v=%s]", type,
obj.getClass().getSimpleName(), obj.getKey(),
hist.getObjVersion()));
}
}
}
}
示例4: removeLocalization
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private void removeLocalization(EntityPersister persister, Object entity) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
List<LocalizedObjectTypes> objType = obj.getLocalizedObjectTypes();
if (objType.size() > 0) {
long key = obj.getKey();
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
org.hibernate.Query query = session
.createQuery("DELETE FROM LocalizedResource WHERE objectKey = :objectKey AND objectType IN (:objectType)");
query.setParameter("objectKey", Long.valueOf(key));
query.setParameterList("objectType", objType);
query.executeUpdate();
tx.commit();
session.close();
}
}
}
示例5: createHistory
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private void createHistory(EntityPersister persister, Object entity,
ModificationType type) {
if (entity instanceof DomainObject<?>) {
DomainObject<?> obj = (DomainObject<?>) entity;
if (obj.hasHistory()) {
final DomainHistoryObject<?> hist = HistoryObjectFactory
.create(obj, type,
DataServiceBean.getCurrentHistoryUser());
final StatelessSession session = persister.getFactory()
.openStatelessSession();
Transaction tx = session.beginTransaction();
session.insert(hist);
tx.commit();
session.close();
if (logger.isDebugLoggingEnabled()) {
logger.logDebug(String.format("%s %s[%s, v=%s]", type, obj
.getClass().getSimpleName(), Long.valueOf(obj
.getKey()), Long.valueOf(hist.getObjVersion())));
}
}
}
}
示例6: testRefresh
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testRefresh() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Paper paper = new Paper();
paper.setColor( "whtie" );
ss.insert( paper );
tx.commit();
ss.close();
ss = getSessions().openStatelessSession();
tx = ss.beginTransaction();
Paper p2 = ( Paper ) ss.get( Paper.class, paper.getId() );
p2.setColor( "White" );
ss.update( p2 );
tx.commit();
ss.close();
ss = getSessions().openStatelessSession();
tx = ss.beginTransaction();
assertEquals( "whtie", paper.getColor() );
ss.refresh( paper );
assertEquals( "White", paper.getColor() );
ss.delete( paper );
tx.commit();
ss.close();
}
示例7: usingStatelessSession
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
static boolean usingStatelessSession(Consumer<StatelessSession> action) {
if (sessionFactory != null) {
StatelessSession session = sessionFactory.openStatelessSession();
try {
Transaction transaction = session.beginTransaction();
action.accept(session);
transaction.commit();
return true;
} catch (Throwable e) {
return false;
} finally {
session.close();
}
}
return false;
}
示例8: executeTransactional
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
/**
* 지정한 session으로부터 StatelessSession을 생성한 후 작업을 수행하고, 닫습니다. @param session the session
*
* @param action the action
*/
public static void executeTransactional(Session session, Action1<StatelessSession> action) {
if (log.isDebugEnabled())
log.debug("StatelessSession을 이용하여 Transaction 하에서 특정 작업을 수행합니다.");
StatelessSession stateless = openStatelessSession(session);
Transaction tx = null;
try {
tx = stateless.beginTransaction();
action.perform(stateless);
tx.commit();
} catch (Exception e) {
log.error("StatelessSession을 이용한 작업에 실패했습니다. rollback 합니다.", e);
if (tx != null)
tx.rollback();
throw new RuntimeException(e);
} finally {
stateless.close();
}
}
示例9: execute
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
/**
* Execute the actions.
*
* @param session the session
* @param actions the actions
*/
public static void execute(Session session, Iterable<Action1<StatelessSession>> actions) {
if (log.isDebugEnabled())
log.debug("StatelessSession을 이용하여 특정 작업을 수행합니다.");
StatelessSession stateless = openStatelessSession(session);
try {
for (Action1<StatelessSession> action : actions)
action.perform(stateless);
} catch (Exception e) {
log.error("StatelessSession에서 작업이 실패했습니다.", e);
throw new RuntimeException(e);
} finally {
stateless.close();
}
}
示例10: testHqlBulk
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testHqlBulk() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Document doc = new Document("blah blah blah", "Blahs");
ss.insert(doc);
Paper paper = new Paper();
paper.setColor( "White" );
ss.insert(paper);
tx.commit();
tx = ss.beginTransaction();
int count = ss.createQuery( "update Document set name = :newName where name = :oldName" )
.setString( "newName", "Foos" )
.setString( "oldName", "Blahs" )
.executeUpdate();
assertEquals( "hql-update on stateless session", 1, count );
count = ss.createQuery( "update Paper set color = :newColor" )
.setString( "newColor", "Goldenrod" )
.executeUpdate();
assertEquals( "hql-update on stateless session", 1, count );
tx.commit();
tx = ss.beginTransaction();
count = ss.createQuery( "delete Document" ).executeUpdate();
assertEquals( "hql-delete on stateless session", 1, count );
count = ss.createQuery( "delete Paper" ).executeUpdate();
assertEquals( "hql-delete on stateless session", 1, count );
tx.commit();
ss.close();
}
示例11: testInitId
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void testInitId() {
StatelessSession ss = getSessions().openStatelessSession();
Transaction tx = ss.beginTransaction();
Paper paper = new Paper();
paper.setColor( "White" );
ss.insert(paper);
assertNotNull( paper.getId() );
tx.commit();
tx = ss.beginTransaction();
ss.delete( ss.get( Paper.class, paper.getId() ) );
tx.commit();
ss.close();
}
示例12: exists
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
@Override
public String exists(final String tenantId,
final String name) {
logger.trace(ORM_LOG_MARKER, "exists(...) entering...");
StatelessSession session = null;
try {
session = sessionFactory.openStatelessSession();
List<?> ids = session
.createCriteria(AlarmDefinitionDb.class)
.add(Restrictions.eq("tenantId", tenantId))
.add(Restrictions.eq("name", name))
.add(Restrictions.isNull("deletedAt"))
.setProjection(Projections.property("id"))
.setMaxResults(1)
.list();
final String existingId = CollectionUtils.isEmpty(ids) ? null : (String) ids.get(0);
if (null == existingId) {
logger.debug(ORM_LOG_MARKER, "No AlarmDefinition matched tenantId={} and name={}", tenantId, name);
}
return existingId;
} finally {
if (session != null) {
session.close();
}
}
}
示例13: findAlarmIds
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public List<String> findAlarmIds(String tenantId, Map<String, String> dimensions) {
logger.trace(BaseSqlRepo.ORM_LOG_MARKER, "findAlarmIds(...) entering");
List<String> alarmIdList = null;
StatelessSession session = null;
try {
session = sessionFactory.openStatelessSession();
final String sql = this.findAlarmQueryString(dimensions);
final Query query = session
.createSQLQuery(sql)
.setString("tenantId", tenantId);
this.bindDimensionsToQuery(query, dimensions);
@SuppressWarnings("unchecked") List<Object[]> rows = query.list();
alarmIdList = Lists.newArrayListWithCapacity(rows.size());
for (Object[] row : rows) {
String id = (String) row[0];
alarmIdList.add(id);
}
} finally {
if (session != null) {
session.close();
}
}
// no need to check if alarmIdList != null, because in case of exception method
// will leave immediately, otherwise list wont be null.
return alarmIdList;
}
示例14: saveInventory
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
public void saveInventory() throws HibernateException, IOException{
File inventoryFile = new File("ghcnm.inv");
BufferedReader br = new BufferedReader(new FileReader(inventoryFile));
String line;
org.hibernate.Session session =
(org.hibernate.Session)GeoInventory.em().getDelegate();
StatelessSession stateless = session.getSessionFactory().openStatelessSession();
try {
stateless.connection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
org.hibernate.Transaction tx = stateless.beginTransaction();
int counter=0;
while ((line = br.readLine()) != null) {
counter++;
GeoInventory inv = new GeoInventory(line);
stateless.insert(inv);
if(counter%1000==0){
tx.commit();
tx = stateless.beginTransaction();
Logger.info("Data Saved: %s", counter);
}
}
tx.commit();
br.close();
stateless.close();
session.clear();
Logger.info("Geoloc Inventory save complete!");
}
示例15: closeSession
import org.hibernate.StatelessSession; //导入方法依赖的package包/类
private void closeSession(StatelessSession session) {
session.close();
}