Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use try-with-resources in Spring 5 documentations #22269

Closed
pashazadeh opened this issue Jan 16, 2019 · 0 comments
Closed

Use try-with-resources in Spring 5 documentations #22269

pashazadeh opened this issue Jan 16, 2019 · 0 comments
Assignees
Labels
type: documentation A documentation task
Milestone

Comments

@pashazadeh
Copy link

Spring 5 supports JDK 8+, so it is much better to change the sample codes to Java 7 try-with-resources instead of old fashion style.

For example in 4.4.2. Implementing DAOs Based on JPA: EntityManagerFactory and EntityManager there exists following code

EntityManager em = this.emf.createEntityManager();
try {
    Query query = em.createQuery("from Product as p where p.category = ?1");
    query.setParameter(1, category);
    return query.getResultList();
} finally {
    if (em != null) {
        em.close();
    }
}

which could be replaced with

try(EntityManager em = this.emf.createEntityManager()) {
    Query query = em.createQuery("from Product as p where p.category = ?1");
    query.setParameter(1, category);
    return query.getResultList();
}

The same thing applies to OXM samples, and many more.

@bclozel bclozel added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 17, 2019
@rstoyanchev rstoyanchev self-assigned this Jan 17, 2019
@rstoyanchev rstoyanchev added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 17, 2019
@rstoyanchev rstoyanchev added this to the 5.1.5 milestone Jan 17, 2019
sbrannen pushed a commit to sbrannen/spring-framework that referenced this issue Jan 4, 2022
The EntityManager interface does not implement AutoCloseable until
JPA 3.1.

This commit therefore partially reverts 189e1af so that the
example code compiles with the supported JPA version.

See spring-projectsgh-22269
Closes spring-projectsgh-27886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

3 participants