Dan*_*ons 5 ejb mybatis java-ee-6
我是Java EE 6的新手,所以如果这个问题的答案显而易见,我会道歉.我有一个必须每小时运行一次从数据库重建Solr索引的任务.我还希望在部署应用程序时进行重建.我的直觉是这应该有效:
@Singleton
@Startup
public class Rebuilder {
@Inject private ProposalDao proposalDao;
@Inject private SolrServer solrServer;
@Schedule(hour="*", minute="0", second="0")
public void rebuildIndex() {
// do the rebuild here
}
}
Run Code Online (Sandbox Code Playgroud)
因为我正在使用myBatis,所以我写了这个制作人:
public class ProposalSessionProvider {
private static final String CONFIGURATION_FILE = "...";
static {
try {
sessFactory = new SqlSessionFactoryBuilder().build(
Resources.getResourceAsReader(CONFIGURATION_FILE));
}
catch (IOException ex) {
throw new RuntimeException("Error configuring MyBatis: " + ex.getMessage(), ex);
}
}
@Produces
public ProposalsDao openSession() {
log.info("Connecting to the database");
session = sessFactory.openSession();
return session.getMapper(ProposalsDao.class);
}
}
Run Code Online (Sandbox Code Playgroud)
所以我有三个问题:
@PostConstruct方法?@Singleton连接将永远不会被释放,但它是否有意义地放在@Startup一个@Statelessbean?@PostConstruct用来处理初始重建或者我每小时都会进行双重重建.我不确定如何继续这里.谢谢你的时间.
我不知道 myBatis,但我可以告诉你 @Schedule 工作是事务性的。无论如何,我不确定 JTA 管理的事务是否会根据您检索会话的方式应用于此处。
没有办法在 MyBatis 中检索 persistenceContext 吗?
对于触发器部分,恕我直言,@Startup 将正确完成工作,并且需要一个单例 bean。无论如何,我无法告诉您您提出的两种方法中哪一种是最好的。
| 归档时间: |
|
| 查看次数: |
285 次 |
| 最近记录: |