session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { //connection, finally! } });
switch ( work.getType() ) { case ADD: operation = interceptor.onAdd( work.getEntity() ); break; case UPDATE: operation = interceptor.onUpdate( work.getEntity() ); break; case DELETE: operation = interceptor.onDelete( work.getEntity() ); break; case COLLECTION: operation = interceptor.onCollectionUpdate( work.getEntity() ); break; case PURGE: break; default: throw new AssertionFailure( "Unknown work type: " + work.getType() ); IndexedTypeIdentifier entityType = work.getTypeIdentifier(); switch ( operation ) { case APPLY_DEFAULT: case SKIP: result = null; log.forceSkipIndexOperationViaInterception( entityType, work.getType() ); break; case UPDATE: result = new Work( work.getTenantIdentifier(), work.getEntity(), work.getId(), WorkType.UPDATE );
ArrayList<Work> listOfJobs = new ArrayList<Work>(); ArrayList<Thread> threadList = new ArrayList<Thread>(); for(int i = 0 ; i < 10; i++) { Work w = new Work(); listOfJobs.add(w); Thread t = new Thread(w); threadList.add(t); t.start(); } for(Thread t : listOfJobs) { t.join(); w.getResultsFromThread(); }
/** * We need to make a difference on which value is used as identifier * according to use case and mapping options * * @param work The work instance from which to extract the id * * @return the appropriate id to use for this work */ private Serializable extractProperId(Work work) { // see HSEARCH-662 if ( containedInOnly ) { return work.getId(); } Object entity = work.getEntity(); // 1) entity is null for purge operation, which requires to trust the work id // 2) types mapped as provided id require to use the work id // 3) when Hibernate identifier rollback is used && this identifier is our same id source, we need to get the value from work id if ( entity == null || documentBuilder.requiresProvidedId() || ( work.isIdentifierWasRolledBack() && documentBuilder.isIdMatchingJpaId() ) ) { return work.getId(); } else { return documentBuilder.getId( entity ); } }
entity = work.getEntity(); tenantId = work.getTenantIdentifier(); WorkType type = work.getType();
@Override public IndexedTypeIdentifier getIndexedTypeIdFromWork(Work work) { return work.getTypeIdentifier() != null ? work.getTypeIdentifier() : new PojoIndexedTypeIdentifier( getClass( work.getEntity() ) ); }
/** * Adds a work to the current plan. The entityClass of the work must be of the * type managed by this. * * @param work the {@code Work} instance to add to the plan */ public void addWork(Work work) { if ( work.getType() == WorkType.PURGE_ALL ) { entityById.clear(); this.deletionQueries.clear(); purgeAll = true; } else if ( work.getType() == WorkType.DELETE_BY_QUERY ) { DeleteByQueryWork delWork = (DeleteByQueryWork) work; this.deletionQueries.add( delWork.getDeleteByQuery() ); } else { Serializable id = extractProperId( work ); PerEntityWork entityWork = entityById.get( id ); if ( entityWork == null ) { entityWork = new PerEntityWork( work ); entityById.put( id, entityWork ); } entityWork.addWork( work ); } }
switch ( work.getType() ) { case ADD: operation = interceptor.onAdd( work.getEntity() ); break; case UPDATE: operation = interceptor.onUpdate( work.getEntity() ); break; case DELETE: operation = interceptor.onDelete( work.getEntity() ); break; case COLLECTION: operation = interceptor.onCollectionUpdate( work.getEntity() ); break; case PURGE: break; default: throw new AssertionFailure( "Unknown work type: " + work.getType() ); IndexedTypeIdentifier entityType = work.getTypeIdentifier(); switch ( operation ) { case APPLY_DEFAULT: case SKIP: result = null; log.forceSkipIndexOperationViaInterception( entityType, work.getType() ); break; case UPDATE: result = new Work( work.getTenantIdentifier(), work.getEntity(), work.getId(), WorkType.UPDATE );
/** * We need to make a difference on which value is used as identifier * according to use case and mapping options * * @param work The work instance from which to extract the id * * @return the appropriate id to use for this work */ private Serializable extractProperId(Work work) { // see HSEARCH-662 if ( containedInOnly ) { return work.getId(); } Object entity = work.getEntity(); // 1) entity is null for purge operation, which requires to trust the work id // 2) types mapped as provided id require to use the work id // 3) when Hibernate identifier rollback is used && this identifier is our same id source, we need to get the value from work id if ( entity == null || documentBuilder.requiresProvidedId() || ( work.isIdentifierWasRolledBack() && documentBuilder.isIdMatchingJpaId() ) ) { return work.getId(); } else { return documentBuilder.getId( entity ); } }
entity = work.getEntity(); tenantId = work.getTenantIdentifier(); WorkType type = work.getType();
@Override public IndexedTypeIdentifier getIndexedTypeIdFromWork(Work work) { return work.getTypeIdentifier() != null ? work.getTypeIdentifier() : new PojoIndexedTypeIdentifier( getClass( work.getEntity() ) ); }
/** * Calculates the expect number of documents in the index by replaying * the work log, taking into account deletes, add and updates * * @return index size */ public int calculateIndexSize() { Set<Serializable> added = new HashSet<>(); for ( Work work : workLog ) { if ( work.getType().equals( DELETE ) ) { added.remove( work.getId() ); } else { added.add( work.getId() ); } } return added.size(); } }
/** * Adds a work to the current plan. The entityClass of the work must be of the * type managed by this. * * @param work the {@code Work} instance to add to the plan */ public void addWork(Work work) { if ( work.getType() == WorkType.PURGE_ALL ) { entityById.clear(); this.deletionQueries.clear(); purgeAll = true; } else if ( work.getType() == WorkType.DELETE_BY_QUERY ) { DeleteByQueryWork delWork = (DeleteByQueryWork) work; this.deletionQueries.add( delWork.getDeleteByQuery() ); } else { Serializable id = extractProperId( work ); PerEntityWork entityWork = entityById.get( id ); if ( entityWork == null ) { entityWork = new PerEntityWork( work ); entityById.put( id, entityWork ); } entityWork.addWork( work ); } }
Session session = entityManager.unwrap(Session.class); session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { // do whatever you need to do with the connection } });