我目前遇到的問題是,@Transactional
註解似乎並沒有啓動Neo4j的交易,但(這不只是我的任何@Transactional註解的方法工作,而不是沒有效果用下面的例子)。@Transactional有
實施例:
我有此方法(UserService.createUser
),它會在Neo4j的圖表第一用戶節點,然後在MongoDB中創建用戶(具有附加信息)。 (MongoDB不支持事務,因此首先創建用戶節點,然後將實體插入到MongoDB中,然後提交Neo4j事務)。
該方法用@Transactional
進行註釋,但在創建Neo4j中的用戶時引發了org.neo4j.graphdb.NotInTransactionException
。
下面分別約爲我的配置和編碼:
代碼基於SDN-Neo4j的配置:
@Configuration
@EnableTransactionManagement // mode = proxy
@EnableNeo4jRepositories(basePackages = "graph.repository")
public class Neo4jConfig extends Neo4jConfiguration {
private static final String DB_PATH = "path_to.db";
private static final String CONFIG_PATH = "path_to.properties";
@Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(DB_PATH)
.loadPropertiesFromFile(CONFIG_PATH).newGraphDatabase();
}
}
服務用於創建Neo4j的用戶和MongoDB的:
@Service
public class UserService {
@Inject
private UserMdbRepository mdbUserRepository; // MongoRepository
@Inject
private Neo4jTemplate neo4jTemplate;
@Transactional
public User createUser(User user) {
// Create the graph-node first, because if this fails the user
// shall not be created in the MongoDB
this.neo4jTemplate.save(user); // NotInTransactionException is thrown here
// Then create the MongoDB-user. This can't be rolled back, but
// if this fails, the Neo4j-modification shall be rolled back too
return this.mdbUserRepository.save(user);
}
...
}
側面說明:
- 我使用Spring版本
3.2.3.RELEASE
和彈簧數據Neo4j的版本2.3.0.M1
- UserService和Neo4jConfig在單獨的Maven構件
- 啓動服務器和SDN讀取操作遠工作,所以,我只是在編寫操作時遇到問題
- 我目前正在將我們的項目從tinkerpop-framework遷移到SDN-Neo4j。這個用戶創建過程之前(與tinkerpop)一起工作,我只需要再次使用SDN-Neo4j就可以工作。
- 我跑在碼頭 應用
有沒有人有這是爲什麼不工作(但)任何線索?
我希望,這些信息就足夠了。如果缺少任何內容,請讓我知道,我會添加它。
編輯:
我忘了提,手動事務處理的作品,但我當然想實現它的方式「因爲它的意思是」。
public User createUser(User user) throws ServiceException {
Transaction tx = this.graphDatabaseService.beginTx();
try {
this.neo4jTemplate.save(user);
User persistantUser = this.mdbUserRepository.save(user);
tx.success();
return persistantUser;
} catch (Exception e) {
tx.failure();
throw new ServiceException(e);
} finally {
tx.finish();
}
}
您的'TransactionManager' bean在哪裏? –
在超類'org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTransactionManager()' – Prjio
如何創建UserService的實例?你能升級到2.3.RC1嗎?哦,如果您對服務器使用SDN,您將不會那麼開心,可能最好是主要通過Cypher使用您的SDN代碼,或者使用SDN和嵌入式API實現服務器擴展。未來計劃爲遠程服務器和SDN提供更好的性能。 –