FullTextEntityManager Session Closed Sleep Search

I am trying to use the FullTextEntityManager (and Spring), but the Session is closed exception. The first time I can request a fine, but the second time, an exception is thrown. Here is my configuration:

@Service
@Transactional(readOnly = true, propagation=Propagation.SUPPORTS)
public class SearchServiceImpl extends BaseService implements SearchService {

    public List<StrainSearchResultsListItem> advancedSearch(Pageable page,String species) {
      return searchRepository.advancedSearch(page,   species);
   }

Repo impl:

@Repository
@Transactional(readOnly = true)
public class SearchRepositoryImpl implements SearchRepository {

  @PersistenceContext
   public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
   }

   protected FullTextEntityManager getFullTextEntityManager() {

      if (fullTextEntityManager == null) {
         fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
      }

      return fullTextEntityManager;
   }

As soon as I call fullTestQuery.getResultList () a second time, it throws with the exception "Session closed".

FullTextQuery fullTextQuery = 
         getFullTextEntityManager()
            .createFullTextQuery(booleanQuery, Strain.class);
fullTextQuery.getResultList()

Any ideas are welcome.

thank

+5
source share
2 answers

You may have forgotten to include TransactionManagement in the spring configuration file. @EnableTransactionManagement to spring configuration file to enable transaction management.

0
source

this-

@Entity
@Table(name="keywordsentity")
@Indexed
@AnalyzerDef(
          name="fulltext",
          tokenizer=@TokenizerDef(factory=StandardTokenizerFactory.class),
          filters={
            @TokenFilterDef(factory=LowerCaseFilterFactory.class),
            @TokenFilterDef(factory=SnowballPorterFilterFactory.class, 
              params={@Parameter(name="language", value="English") })
          }
        )
public class Keywordsentity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @JsonProperty
    //index = Index.YES, analyze=Analyze.YES, store = Store.YES
    @Field
    @Analyzer(definition="fulltext") 
    private String keywordname;

@Service
public class KeywordService {


    @PersistenceContext(type = PersistenceContextType.EXTENDED, name = "keywordPU")
    private EntityManager em;

    private FullTextEntityManager ftem;

      public void updateFullTextIndex() throws Exception {

            getFullTextEntityManager().createIndexer().startAndWait();
        }



    protected FullTextEntityManager getFullTextEntityManager() {
        if (ftem == null) {
            ftem = Search.getFullTextEntityManager(em);
        }
        return ftem;
    }

    @Transactional
      public List<Keywordsentity> search(String summary, String description)
      {


          String searchString = summary.concat(" ").concat(description);
          System.out.println("searchString-----------------------------"+searchString);
          QueryBuilder qb = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(Keywordsentity.class).get();


    //lucene query
          org.apache.lucene.search.Query query = qb
                    .keyword()
                    .onField("keywordname").matching(searchString)
                    .createQuery();

            Query fullTextQuery = getFullTextEntityManager().createFullTextQuery(query, Keywordsentity.class);

            System.out.println("fullTextQuery------------------================="+fullTextQuery);

            List<Keywordsentity> result = new ArrayList<Keywordsentity>();
            try
            {
                 result = fullTextQuery.getResultList();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }



            System.out.println("size --------------------=========="+result.size());
            for (Keywordsentity keywordone : result) {
             System.out.println("keyword------------"+keywordone);
             Map<String,String> team =new HashMap<String,String>();
         Set<Teamsentity> teams=     keywordone.getTeamsentity();
         {
             for(Teamsentity teamsentityone :teams )
            {
                String ids = String.valueOf(teamsentityone.getId());
                team.put("id",ids);
                team.put("name",teamsentityone.getName());
                team.put("description",teamsentityone.getDescription());
            }
             System.out.println("teams =================="+teams);

             }

            }
            return result;
      }

}

, . -

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.10.5.Final</version>
    </dependency>
0

Source: https://habr.com/ru/post/1535059/


All Articles