gpt4 book ai didi

java - Spring Standalone无法识别@Transactional

转载 作者:行者123 更新时间:2023-12-01 08:58:50 26 4
gpt4 key购买 nike

最近,我将 Spring 集成到我的 Jboss 应用程序中。我还组装了一个独立的应用程序,它将使用一些 Spring 服务类。这是我的独立源:

@Component
public class StandaloneTest {

public static ApplicationContext context;

public static void main(String[] args) {
context = new AnnotationConfigApplicationContext(StandaloneSpringBeanConfiguration.class);
StandaloneTest test = context.getBean(StandaloneTest.class);
System.out.println(context);
test.startProcess();
}

public StandaloneTest() {

}

/**
* Start the main process
*/
private void startProcess() {
run();
}

private void run() {
TestService testService = context.getBean(TestService.class);
testService.transactionalTest();
System.out.println("finished");
}

这是 TestService 的来源:

@Service
public class TestServiceImpl implements TestService {

public GeneralDAO generalDAO;

private EntityManager em;

private EntityManagerFactory emf;

public TestServiceImpl(){}

@Transactional
public void transactionalTest() {
Something something = new Something();
getEntityManager().persist(something);
}

public EntityManager getEntityManager() {
if (em == null) {
emf = Persistence.createEntityManagerFactory("xxx");
em = emf.createEntityManager();
}
return em;
}


}

当我在 Jboss 服务器中运行此方法时,@Transactional 注释按预期工作。但是,当我运行独立应用程序时,@Transactional 注释似乎被忽略。我不明白为什么。

如果有人知道我需要做什么才能使其发挥作用或有任何想法/评论,请分享。谢谢!

更新

这是我的配置文件:

@Configuration
@ComponentScan
@EnableTransactionManagement
public class StandaloneSpringBeanConfiguration {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPersistenceUnitName("myPersistenceContext");
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "xxx.xxx.xxx"});

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// em.setJpaProperties(additionalProperties());

return em;
}

@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jboss_test");
dataSource.setUsername( "root" );
dataSource.setPassword( "root" );
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager(emf);
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}


@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}

更多更新

@Service
public class TestServiceImpl implements TestService {

public GeneralDAO generalDAO;

@PersistenceContext
private EntityManager em;

private EntityManagerFactory emf;

public TestServiceImpl(){}

@Transactional
public void transactionalTest() {
Something something = new Something();
em.persist(something);
}

}

这绝对让我朝着正确的方向前进。我现在收到此错误:没有可用的 JTA UserTransaction - 请指定“userTransaction”或“userTransactionName”或“transactionManager”或“transactionManagerName”

我无法找出原因,但问题是我选择的 transactionManager 吗?我在某处读到我需要使用 JOTM 或 Atomikos 进行独立,但我不认为 JOTM 可以在 Spring 4 中使用,而 Atomikos 要求我使用 Maven,这对我来说不是一个选择。使用 JTATransactionManager 给了我同样的结果。

工作解决方案

今天早上 JTA 错误似乎已自行解决。不知道发生了什么。然后,我必须确保 LocalContainerEntityManagerFactoryBean 扫描所有必需的包,因为我收到了 IllegalArgumentException Unknown Entity

配置

public class StandaloneSpringBeanConfiguration {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "xxx.xxx.xxx", "xxx.xxx.xxx"});

JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// em.setJpaProperties(additionalProperties());

return em;
}

@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/jboss_test");
dataSource.setUsername( "root" );
dataSource.setPassword( "root" );
return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}


@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}

服务等级

@Service
public class TestServiceImpl implements TestService {

public GeneralDAO generalDAO;

@PersistenceContext
private EntityManager em;

private EntityManagerFactory emf;

public TestServiceImpl(){}

@Transactional
public void transactionalTest() {
Something something = new Something();
em.persist(something);
}

}

最佳答案

交易工作正常,只是在您的设置中不起作用。这是因为您正在重新创建事务绑定(bind) EntityManager,从而解决它。

基本上,您的 TestServiceImpl 代码有缺陷。您应该删除 getEntityManager() 方法,并使用 @PersistenceContext 注释 EntityManager 字段。

@Service
public class TestServiceImpl implements TestService {

public GeneralDAO generalDAO;

@PersistenceContext
private EntityManager em;

public TestServiceImpl(){}

@Transactional
public void transactionalTest() {
Something something = new Something();
em.persist(something);
}
}

这就是您所需要的。

关于java - Spring Standalone无法识别@Transactional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com