-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear default transaction from thread local when the query fails
Fixes #393
- Loading branch information
1 parent
1bf4f9c
commit a3b17c2
Showing
4 changed files
with
155 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
test/src/test/java/org/neo4j/ogm/persistence/transaction/DefaultTransactionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2002-2017 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this product except in compliance with the License. | ||
* | ||
* This product may include a number of subcomponents with | ||
* separate copyright notices and license terms. Your use of the source | ||
* code for these subcomponents is subject to the terms and | ||
* conditions of the subcomponent's license, as noted in the LICENSE file. | ||
*/ | ||
|
||
package org.neo4j.ogm.persistence.transaction; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.neo4j.ogm.domain.simple.User; | ||
import org.neo4j.ogm.model.Result; | ||
import org.neo4j.ogm.session.Session; | ||
import org.neo4j.ogm.session.SessionFactory; | ||
import org.neo4j.ogm.testutil.MultiDriverTestClass; | ||
|
||
import static java.util.Collections.emptyMap; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
||
/** | ||
* Test of behaviour of the Session for default transactions (transactions without explicit handling) | ||
* | ||
* @author Frantisek Hartman | ||
*/ | ||
public class DefaultTransactionTest extends MultiDriverTestClass { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultTransactionTest.class); | ||
|
||
private static SessionFactory sessionFactory; | ||
private Session session; | ||
|
||
@BeforeClass | ||
public static void setUpClass() throws Exception { | ||
sessionFactory = new SessionFactory(driver, "org.neo4j.ogm.domain.simple"); | ||
} | ||
|
||
@Before | ||
public void init() throws IOException { | ||
session = sessionFactory.openSession(); | ||
Result result = session.query("CREATE CONSTRAINT ON (u:User) ASSERT u.name IS UNIQUE", emptyMap()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
session.query("DROP CONSTRAINT ON (u:User) ASSERT u.name IS UNIQUE", emptyMap()); | ||
} | ||
|
||
@Test | ||
public void shouldBeAbleToUseSessionAfterDefaultTransactionFails() throws Exception { | ||
|
||
User u1 = new User("frantisek"); | ||
session.save(u1); | ||
session.clear(); | ||
|
||
try { | ||
session.save(new User("frantisek")); | ||
fail("Constraint violation should have make the second save fail"); | ||
} catch (Exception ex) { | ||
logger.info("Caught exception", ex); | ||
} | ||
|
||
User loaded = session.load(User.class, u1.getId()); | ||
assertThat(loaded).isNotNull(); | ||
} | ||
} |