2013-09-05 50 views
0

我目前遇到的問題是,@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
  • UserServiceNeo4jConfig在單獨的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(); 
     } 
    } 
+1

您的'TransactionManager' bean在哪裏? –

+0

在超類'org.springframework.data.neo4j.config.Neo4jConfiguration.neo4jTransactionManager()' – Prjio

+0

如何創建UserService的實例?你能升級到2.3.RC1嗎?哦,如果您對服務器使用SDN,您將不會那麼開心,可能最好是主要通過Cypher使用您的SDN代碼,或者使用SDN和嵌入式API實現服務器擴展。未來計劃爲遠程服務器和SDN提供更好的性能。 –

回答

0

感謝m-deinum我終於找到了問題。問題在於我在不同的彈簧配置文件中掃描了這些組件/服務,而不是配置了SDN-Neo4j。我搬到了組件掃描這可能需要交易來我Neo4jConfig那些包,現在它的工作原理

@Configuration 
@EnableTransactionManagement      // mode = proxy 
@EnableNeo4jRepositories(basePackages = "graph.repository") 
@ComponentScan({ 
    "graph.component", 
    "graph.service", 
    "core.service" 
}) 
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(); 
    } 
} 

我仍然必須對這些組件/需要從那些沒有交易業務的隔離,雖然。但是,現在這個工作。

我假定,這個問題是,其他彈簧配置文件(其中包括組分掃描)中的溶液Neo4jConfig之前加載,因爲neo4j:repositories具有context:component-scan之前被投入。 (請參閱示例20.26中的注意。撰寫倉庫http://static.springsource.org/spring-data/data-neo4j/docs/current/reference/html/programming-model.html#d0e2948