我有一个Spring启动neo4j应用程序,并希望将用户存储在Neo4j数据库中。我按照here找到的说明进行操作。 neo4j配置如下:
@Configuration
@EnableWebMvc
@ComponentScan({"eu.bm"})
@EnableNeo4jRepositories("eu.bm.repository")
@EnableTransactionManagement
public class bmConfiguration extends WebMvcConfigurerAdapter {
@Bean
public OpenSessionInViewInterceptor openSessionInViewInterceptor() {
OpenSessionInViewInterceptor openSessionInViewInterceptor =
new OpenSessionInViewInterceptor();
openSessionInViewInterceptor.setSessionFactory(sessionFactory());
return openSessionInViewInterceptor;
}
public void addInterceptors(InterceptorRegistry registry) {
registry.addWebRequestInterceptor(openSessionInViewInterceptor());
}
@Bean
public static SessionFactory sessionFactory() {
return new SessionFactory("eu.bm.domain");
}
@Bean
public Neo4jTransactionManager transactionManager() throws Exception {
return new Neo4jTransactionManager(sessionFactory());
}
用户存储库是:
@Repository
public interface UserRepository extends GraphRepository<User>{
@Query("MATCH (user:User) where user.name={0} return user ")
User findUser(String username);
@Query("MATCH (user:User) where user.name={0} delete user ")
User deleteUser(String username);
@Query("match (user:User) delete user")
User deleteAllUsers();
}
我也有此用户管理服务设置:
@Component
public interface UserManagementService {
List<User> listAll();
User save(User user);
User findUser(String username);
}
这是在这里实现的:
@Service
@Transactional
public class UserManagementServiceImpl implements UserManagementService {
private UserRepository userRepository;
@Autowired
public UserManagementServiceImpl(UserRepository userRepository) {this.userRepository = userRepository;}
@Override
public List<User> listAll() {
List<User> users = new ArrayList<>();
userRepository.findAll().forEach(users::add);
return users;
}
@Override
public User save(User user) {
userRepository.save(user);
return user;
}
@Override
public User findUser(String username) {
return userRepository.findUser(username);
}
然后我执行一个简单的写读取测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class DatabaseConnectionTest {
// User 1
private static final String UNAME1 = "Paul";
private static final String EMAIL1 = "paul@user.com";
private static final String PASSWORD1 = "p@ss";
private static final String USERNAME1 = "paul";
@Autowired
private UserRepository userRepository;
@Before
public void setUp() throws Exception {
// Clear database before adding new user
userRepository.deleteAllUsers();
// Creating user
User user = new User(UNAME1, USERNAME1, PASSWORD1, EMAIL1);
userRepository.save(user);
}
@Test
public void testPersistence() {
Assert.assertEquals(UNAME1, userRepository.findUser(UNAME1).getName());
Assert.assertEquals(USERNAME1, userRepository.findUser(UNAME1).getUsername());
}
以上结果是这个错误:
2017-08-16 14:59:38.990 INFO 6496 --- [main] e.b.repository.DatabaseConnectionTest:在7.131秒内启动DatabaseConnectionTest(JVM运行8.331) 2017-08-16 14:59:39.872 INFO 6496 --- [main] onodrivers.http.request.HttpRequest:Thread:1,url:http://localhost:7474/db/data/transaction/39,request:{“statements”:[{“statement “:”UNWIND {rows}作为行CREATE(n:
User
)SET n = row.props RETURN row.nodeRef as ref,ID(n)as id,row.type as type“,”parameters“: { “行”:[{ “nodeRef”: - 1821370276, “类型”: “节点”, “道具”:{ “密码”: “p @ SS”, “名”: “保罗”, “电子邮件”:” paul@user.com”, “用户名”: “保罗”}}]}, “resultDataContents”:[ “行”], “includeStats”:假}]} 2017-08-16 14:59:40.358 ERROR 6496 --- [main] o.s.d.n.t.Neo4jTransactionManager:由回滚异常覆盖的提交异常org.springframework.dao.InvalidDataAccessApiUsageException:此线程的事务不是最新的;嵌套异常是org.neo4j.ogm.exception.TransactionManagerException:此线程的事务不是最新的
编辑:我的dependencies.gradle包含以下内容:
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-neo4j', version: '1.5.6.RELEASE'
//compile group: 'org.springframework.data', name: 'spring-data-neo4j', version: '4.2.6.RELEASE'
compile group: 'org.neo4j', name: 'neo4j-ogm-core', version: '2.1.3'
//compile group: 'org.neo4j', name: 'neo4j-ogm-bolt-driver', version: '2.1.3'
runtime group: 'org.neo4j', name: 'neo4j-ogm-http-driver', version: '2.1.3'
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.4.RELEASE'
testCompile('org.springframework.boot:spring-boot-starter-test')
答案 0 :(得分:2)
您的SessionFactory
bean不应声明为static
:
@Bean
public static SessionFactory sessionFactory() {
return new SessionFactory("eu.bm.domain");
}
应该是
@Bean
public SessionFactory sessionFactory() {
return new SessionFactory("eu.bm.domain");
}
<强>背景强>
“此线程的事务不是最新的”意味着某事正在尝试提交与线程本地上下文中的当前tx不同的事务。
您正在使用默认事务管理 - 它会围绕存储库调用创建一个事务。
当SessionFactory bean被定义为静态时,Session以某种方式看不到线程本地的事务(由默认的tx管理启动的事务)并创建新事务,尝试提交它并出现异常。