我有这个问题:我的应用程序有一个石英调度程序,每X分钟运行一个任务.此应用程序部署在两个服务器实例中,因此每个实例都在同时执行任务.我想同时只执行一项任务.
我们已经使用Spring配置了Quartz,我们的应用服务器是WAS.
你建议哪些选择?
我是JPA的新手.我正在开发一个使用JPA(Hibernate实现)和Spring的应用程序.我在我的persistence.xml中声明了一个持久性单元,并在我的Spring配置文件中配置了关于EntityManagerFactory的配置.像这样的东西:
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="my.package" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
Run Code Online (Sandbox Code Playgroud)
然后我有一些DAO,我用@PersistenceContext注释注入entityManager:
public MyDaoImpl implements MyDao{
private EntityManager entityManager;
@PersistenceContext
private void setEntityManager(EntityManager em){
this.entityManager = em;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一些注入DAO的服务(通过@Autowired Spring的注释):
public MyServiceImpl implements MyService{
@Autowired
private MyDao myDao;
public List<MyEntity> readOperation(){
//
return myDAo.searchAll();
}
}
Run Code Online (Sandbox Code Playgroud)
作为一个只读操作,我认为它不需要@Transactional注释,但没有它,有一个例外:
java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
at $Proxy121.unwrap(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我读过其他一些帖子:java.lang.IllegalStateException:没有可用的事务性EntityManager
所有人都说需要交易注释.这是真的,它适用它,但我想知道(和为什么)所有方法(甚至只读操作)必须是事务性的.