Skip to content

Commit 5323452

Browse files
committed
Bump Hypersistence Optimizer to 2.1.0 and add configuration examples for the Session timeout mechanism
1 parent a17809e commit 5323452

File tree

14 files changed

+183
-65
lines changed

14 files changed

+183
-65
lines changed

README.md

+28-28
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ Once you downloaded the [Full or Trial version](https://vladmihalcea.com/hypersi
99
The first thing you need to do is to unzip the package you have just downloaded.
1010

1111
```bash
12-
> unzip hypersistence-optimizer-2.0.2-pack.zip
12+
> unzip hypersistence-optimizer-2.1.0-pack.zip
1313
```
1414

1515
After unzipping the project package, you will get the following file structure:
1616

1717
```bash
18-
creating: hypersistence-optimizer-2.0.2/
19-
creating: hypersistence-optimizer-2.0.2/lib/
20-
inflating: hypersistence-optimizer-2.0.2/lib/hypersistence-optimizer-2.0.2-javadoc.jar
21-
inflating: hypersistence-optimizer-2.0.2/lib/hypersistence-optimizer-2.0.2-sources.jar
22-
inflating: hypersistence-optimizer-2.0.2/lib/hypersistence-optimizer-2.0.2.jar
23-
creating: hypersistence-optimizer-2.0.2/configs/
24-
creating: hypersistence-optimizer-2.0.2/configs/META-INF/
25-
creating: hypersistence-optimizer-2.0.2/configs/META-INF/services/
26-
inflating: hypersistence-optimizer-2.0.2/configs/META-INF/services/org.hibernate.boot.spi.SessionFactoryBuilderFactory
27-
creating: hypersistence-optimizer-2.0.2/docs/
28-
creating: hypersistence-optimizer-2.0.2/docs/html/
29-
creating: hypersistence-optimizer-2.0.2/docs/pdf/
30-
inflating: hypersistence-optimizer-2.0.2/docs/html/asciidoctor.css
31-
inflating: hypersistence-optimizer-2.0.2/docs/html/coderay-asciidoctor.css
32-
inflating: hypersistence-optimizer-2.0.2/docs/pdf/InstallationGuide.pdf
33-
inflating: hypersistence-optimizer-2.0.2/docs/html/InstallationGuide.html
34-
inflating: hypersistence-optimizer-2.0.2/docs/html/UserGuide.html
35-
inflating: hypersistence-optimizer-2.0.2/docs/pdf/UserGuide.pdf
36-
inflating: hypersistence-optimizer-2.0.2/changelog.txt
37-
inflating: hypersistence-optimizer-2.0.2/LICENSE.txt
38-
inflating: hypersistence-optimizer-2.0.2/maven-install.bat
39-
inflating: hypersistence-optimizer-2.0.2/maven-install.sh
40-
inflating: hypersistence-optimizer-2.0.2/README.txt
18+
creating: hypersistence-optimizer-2.1.0/
19+
creating: hypersistence-optimizer-2.1.0/lib/
20+
inflating: hypersistence-optimizer-2.1.0/lib/hypersistence-optimizer-2.1.0-javadoc.jar
21+
inflating: hypersistence-optimizer-2.1.0/lib/hypersistence-optimizer-2.1.0-sources.jar
22+
inflating: hypersistence-optimizer-2.1.0/lib/hypersistence-optimizer-2.1.0.jar
23+
creating: hypersistence-optimizer-2.1.0/configs/
24+
creating: hypersistence-optimizer-2.1.0/configs/META-INF/
25+
creating: hypersistence-optimizer-2.1.0/configs/META-INF/services/
26+
inflating: hypersistence-optimizer-2.1.0/configs/META-INF/services/org.hibernate.boot.spi.SessionFactoryBuilderFactory
27+
creating: hypersistence-optimizer-2.1.0/docs/
28+
creating: hypersistence-optimizer-2.1.0/docs/html/
29+
creating: hypersistence-optimizer-2.1.0/docs/pdf/
30+
inflating: hypersistence-optimizer-2.1.0/docs/html/asciidoctor.css
31+
inflating: hypersistence-optimizer-2.1.0/docs/html/coderay-asciidoctor.css
32+
inflating: hypersistence-optimizer-2.1.0/docs/pdf/InstallationGuide.pdf
33+
inflating: hypersistence-optimizer-2.1.0/docs/html/InstallationGuide.html
34+
inflating: hypersistence-optimizer-2.1.0/docs/html/UserGuide.html
35+
inflating: hypersistence-optimizer-2.1.0/docs/pdf/UserGuide.pdf
36+
inflating: hypersistence-optimizer-2.1.0/changelog.txt
37+
inflating: hypersistence-optimizer-2.1.0/LICENSE.txt
38+
inflating: hypersistence-optimizer-2.1.0/maven-install.bat
39+
inflating: hypersistence-optimizer-2.1.0/maven-install.sh
40+
inflating: hypersistence-optimizer-2.1.0/README.txt
4141
```
4242

4343
The package contains the following resources:
@@ -55,8 +55,8 @@ The package contains the following resources:
5555
In order to install Hypersistence Optimizer, you need to read the Installation Guide, which is available both in
5656
HTML and PDF formats in the unzipped package:
5757

58-
* `hypersistence-optimizer-2.0.2/docs/pdf/InstallationGuide.pdf`
59-
* `hypersistence-optimizer-2.0.2/docs/html/InstallationGuide.html`
58+
* `hypersistence-optimizer-2.1.0/docs/pdf/InstallationGuide.pdf`
59+
* `hypersistence-optimizer-2.1.0/docs/html/InstallationGuide.html`
6060

6161
> You can also read the [Installation Guide online](https://vladmihalcea.com/hypersistence-optimizer/docs/installation-guide/) if you want.
6262
@@ -67,8 +67,8 @@ Hypersistence Optimizer so that you can get the most out of it.
6767

6868
You can find the User Guide in the docs folder as well:
6969

70-
* `hypersistence-optimizer-2.0.2/docs/html/UserGuide.html`
71-
* `hypersistence-optimizer-2.0.2/docs/pdf/UserGuide.pdf`
70+
* `hypersistence-optimizer-2.1.0/docs/html/UserGuide.html`
71+
* `hypersistence-optimizer-2.1.0/docs/pdf/UserGuide.pdf`
7272

7373
> You can also read the [User Guide online](https://vladmihalcea.com/hypersistence-optimizer/docs/user-guide/) if you want.
7474
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.hypersistence.optimizer.config;
2+
3+
import io.hypersistence.optimizer.HypersistenceOptimizer;
4+
import io.hypersistence.optimizer.core.config.Config;
5+
import io.hypersistence.optimizer.core.config.JpaConfig;
6+
import io.hypersistence.optimizer.core.event.Event;
7+
import io.hypersistence.optimizer.core.event.ListEventHandler;
8+
import io.hypersistence.optimizer.hibernate.event.mapping.association.fetching.EagerFetchingEvent;
9+
import io.hypersistence.optimizer.hibernate.event.session.SessionTimeoutEvent;
10+
import io.hypersistence.optimizer.util.AbstractTest;
11+
import io.hypersistence.optimizer.util.transaction.JPATransactionVoidFunction;
12+
import org.junit.Test;
13+
14+
import javax.persistence.*;
15+
16+
import java.util.Collections;
17+
18+
import static org.junit.Assert.assertSame;
19+
20+
/**
21+
* @author Vlad Mihalcea
22+
*/
23+
public class RuntimeConfigurationPropertiesTest extends AbstractTest {
24+
25+
@Override
26+
public Class<?>[] entities() {
27+
return new Class<?>[]{
28+
};
29+
}
30+
31+
private ListEventHandler listEventHandler = new ListEventHandler();
32+
33+
@Override
34+
protected void afterInit() {
35+
new HypersistenceOptimizer(
36+
new JpaConfig(entityManagerFactory())
37+
.addEventHandler(listEventHandler)
38+
.setProperties(
39+
Collections.singletonMap(
40+
Config.Property.Session.TIMEOUT_MILLIS,
41+
25
42+
)
43+
)
44+
);
45+
}
46+
47+
@Test
48+
public void test() {
49+
doInJPA(entityManager -> {
50+
try {
51+
Thread.sleep(50);
52+
} catch (InterruptedException e) {
53+
Thread.interrupted();
54+
}
55+
});
56+
57+
assertEventTriggered(1, SessionTimeoutEvent.class);
58+
}
59+
60+
protected void assertEventTriggered(int expectedCount, Class<? extends Event> eventClass) {
61+
int count = 0;
62+
63+
for (Event event : listEventHandler.getEvents()) {
64+
if (event.getClass().equals(eventClass)) {
65+
count++;
66+
}
67+
}
68+
69+
assertSame(expectedCount, count);
70+
}
71+
}

hypersistence-optimizer-glassfish-hibernate-example/src/test/java/io/hypersistence/optimizer/forum/adaptor/glassfish/AbstractGlassfishDataSourceJndiIntegrationTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.hypersistence.optimizer.hibernate.event.mapping.association.OneToOneWithoutMapsIdEvent;
1313
import io.hypersistence.optimizer.hibernate.event.mapping.association.fetching.EagerFetchingEvent;
1414
import io.hypersistence.optimizer.hibernate.event.query.PaginationWithoutOrderByEvent;
15+
import io.hypersistence.optimizer.hibernate.event.session.SessionTimeoutEvent;
1516
import org.jboss.arquillian.junit.Arquillian;
1617
import org.junit.Before;
1718
import org.junit.Test;
@@ -60,6 +61,8 @@ public void test() throws Exception {
6061
getEntityManager().persist(post);
6162
});
6263

64+
hypersistenceOptimizer.getEvents().clear();
65+
6366
doInTransaction(() -> {
6467
assertEventTriggered(0, PaginationWithoutOrderByEvent.class);
6568

@@ -72,7 +75,15 @@ public void test() throws Exception {
7275
assertEquals(1, posts.size());
7376

7477
assertEventTriggered(1, PaginationWithoutOrderByEvent.class);
78+
79+
try {
80+
Thread.sleep(25);
81+
} catch (InterruptedException e) {
82+
Thread.interrupted();
83+
}
7584
});
85+
86+
assertEventTriggered(1, SessionTimeoutEvent.class);
7687
}
7788

7889
private void doInTransaction(VoidCallable callable) {

hypersistence-optimizer-glassfish-hibernate-example/src/test/resources-glassfish-embedded/MANIFEST/test-persistence.xml

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626

2727
<properties>
2828
<property name="hibernate.hbm2ddl.auto" value="update"/>
29-
<property
30-
name="hibernate.transaction.jta.platform"
31-
value="SunOne"
32-
/>
29+
<property name="hibernate.transaction.jta.platform" value="SunOne"/>
3330
<property name="hibernate.show_sql" value="true"/>
31+
32+
<property name="hypersistence.session.timeout_millis" value="25"/>
3433
</properties>
3534
</persistence-unit>
3635
</persistence>

hypersistence-optimizer-glassfish-hibernate4-example/src/test/java/io/hypersistence/optimizer/forum/adaptor/glassfish/AbstractGlassfishDataSourceJndiIntegrationTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.hypersistence.optimizer.hibernate.event.mapping.association.OneToOneWithoutMapsIdEvent;
1111
import io.hypersistence.optimizer.hibernate.event.mapping.association.fetching.EagerFetchingEvent;
1212
import io.hypersistence.optimizer.hibernate.event.query.PaginationWithoutOrderByEvent;
13+
import io.hypersistence.optimizer.hibernate.event.session.SessionTimeoutEvent;
1314
import org.jboss.arquillian.junit.Arquillian;
1415
import org.junit.Before;
1516
import org.junit.Test;
@@ -56,6 +57,8 @@ public void test() throws Exception {
5657
getEntityManager().persist(post);
5758
});
5859

60+
hypersistenceOptimizer.getEvents().clear();
61+
5962
doInTransaction(() -> {
6063
assertEventTriggered(0, PaginationWithoutOrderByEvent.class);
6164

@@ -68,7 +71,15 @@ public void test() throws Exception {
6871
assertEquals(1, posts.size());
6972

7073
assertEventTriggered(1, PaginationWithoutOrderByEvent.class);
74+
75+
try {
76+
Thread.sleep(25);
77+
} catch (InterruptedException e) {
78+
Thread.interrupted();
79+
}
7180
});
81+
82+
assertEventTriggered(1, SessionTimeoutEvent.class);
7283
}
7384

7485
private void doInTransaction(VoidCallable callable) {

hypersistence-optimizer-glassfish-hibernate4-example/src/test/resources-glassfish-embedded/MANIFEST/test-persistence.xml

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313

1414
<properties>
1515
<property name="hibernate.hbm2ddl.auto" value="update"/>
16-
16+
<property name="hibernate.transaction.jta.platform" value="SunOne"/>
1717
<property name="hibernate.show_sql" value="true"/>
1818

19-
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
20-
21-
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
19+
<property name="hypersistence.session.timeout_millis" value="25"/>
2220
</properties>
2321
</persistence-unit>
2422
</persistence>

hypersistence-optimizer-spring-boot-example/src/test/java/io/hypersistence/optimizer/forum/ApplicationTest.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.hypersistence.optimizer.hibernate.event.mapping.association.OneToOneWithoutMapsIdEvent;
3232
import io.hypersistence.optimizer.hibernate.event.mapping.association.fetching.EagerFetchingEvent;
3333
import io.hypersistence.optimizer.hibernate.event.query.PaginationWithoutOrderByEvent;
34+
import io.hypersistence.optimizer.hibernate.event.session.SessionTimeoutEvent;
3435
import org.junit.Before;
3536
import org.junit.Test;
3637
import org.junit.runner.RunWith;
@@ -43,12 +44,12 @@
4344
import org.springframework.transaction.support.TransactionCallback;
4445
import org.springframework.transaction.support.TransactionTemplate;
4546

46-
import javax.persistence.EntityManager;
47-
import javax.persistence.EntityManagerFactory;
48-
import javax.persistence.PersistenceContext;
49-
import javax.persistence.PersistenceUnit;
47+
import javax.persistence.*;
5048
import java.util.Arrays;
5149
import java.util.List;
50+
import java.util.concurrent.ExecutionException;
51+
import java.util.concurrent.ExecutorService;
52+
import java.util.concurrent.Executors;
5253

5354
import static org.junit.Assert.*;
5455

@@ -70,6 +71,12 @@ public class ApplicationTest {
7071
@Autowired
7172
private ForumService forumService;
7273

74+
protected final ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {
75+
Thread bob = new Thread(r);
76+
bob.setName("Bob");
77+
return bob;
78+
});
79+
7380
@Before
7481
public void init() {
7582
try {
@@ -89,7 +96,7 @@ public void init() {
8996
}
9097

9198
@Test
92-
public void test() {
99+
public void test() throws ExecutionException, InterruptedException {
93100
assertEventTriggered(2, EagerFetchingEvent.class);
94101
assertEventTriggered(1, ManyToManyListEvent.class);
95102
assertEventTriggered(1, OneToOneParentSideEvent.class);
@@ -99,18 +106,37 @@ public void test() {
99106
assertEventTriggered(1, QueryPaginationCollectionFetchingEvent.class);
100107
assertEventTriggered(1, QueryInClauseParameterPaddingEvent.class);
101108

102-
Post newPost = forumService.newPost("High-Performance Java Persistence", Arrays.asList("hibernate", "jpa"));
103-
assertNotNull(newPost.getId());
109+
Post newPost = null;
110+
111+
for (int i = 0; i < 10; i++) {
112+
newPost = forumService.newPost("High-Performance Java Persistence", Arrays.asList("hibernate", "jpa"));
113+
assertNotNull(newPost.getId());
114+
}
104115

105116
List<Post> posts = forumService.findAllByTitle("High-Performance Java Persistence");
106-
assertEquals(1, posts.size());
117+
assertEquals(10, posts.size());
107118

108119
Post post = forumService.findById(newPost.getId());
109120
assertEquals("High-Performance Java Persistence", post.getTitle());
110121

111122
assertEventTriggered(0, PaginationWithoutOrderByEvent.class);
112-
assertEquals(1, forumService.findAll(5).size());
123+
assertEquals(5, forumService.findAll(5).size());
113124
assertEventTriggered(1, PaginationWithoutOrderByEvent.class);
125+
126+
hypersistenceOptimizer.getEvents().clear();
127+
128+
executorService.submit(() -> transactionTemplate.execute(
129+
transactionStatus -> {
130+
try {
131+
Thread.sleep(50);
132+
} catch (InterruptedException e) {
133+
Thread.interrupted();
134+
}
135+
return null;
136+
}
137+
)).get();
138+
139+
assertEventTriggered(1, SessionTimeoutEvent.class);
114140
}
115141

116142
protected void assertEventTriggered(int expectedCount, Class<? extends Event> eventClass) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.jpa.properties.hypersistence.session.timeout_millis=25
2+
spring.jpa.open-in-view=false

hypersistence-optimizer-test-case/src/test/java/io/hypersistence/optimizer/util/transaction/HibernateTransactionConsumer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
/**
66
* @author Vlad Mihalcea
77
*/
8-
public abstract class HibernateTransactionConsumer {
9-
public abstract void accept(Session t);
8+
public interface HibernateTransactionConsumer {
9+
void accept(Session t);
1010

11-
public void beforeTransactionCompletion() {
11+
default void beforeTransactionCompletion() {
1212

1313
}
1414

15-
public void afterTransactionCompletion() {
15+
default void afterTransactionCompletion() {
1616

1717
}
1818
}

hypersistence-optimizer-test-case/src/test/java/io/hypersistence/optimizer/util/transaction/HibernateTransactionFunction.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
/**
66
* @author Vlad Mihalcea
77
*/
8-
public abstract class HibernateTransactionFunction<T> {
8+
public interface HibernateTransactionFunction<T> {
99

10-
public abstract T apply(Session t);
10+
T apply(Session t);
1111

12-
public void beforeTransactionCompletion() {
12+
default void beforeTransactionCompletion() {
1313

1414
}
1515

16-
public void afterTransactionCompletion() {
16+
default void afterTransactionCompletion() {
1717

1818
}
1919
}

0 commit comments

Comments
 (0)