获取javax.persistence.TransactionRequiredException:没有正在进行的事务错误,但是我可以在@Transactional函数中打印查询的数据,但是在返回到其余控制器时却遇到此错误。
我正在连接到两个数据库,并且为每个数据库创建了HibernateTransactionManager。对于@Primary Transaction bean,它能够将响应返回给rest控制器,但不能返回到secondary bean
提前谢谢!! 我要花几个小时才能解决这个问题。
db1Config.java
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("au.net.nbnco.mwoRoutingAPI")
@PropertySource({ "classpath:persistence-oracle-db.properties" })
public class RoutingAppConfigCDL implements WebMvcConfigurer {
@Autowired
private Environment env;
private Logger logger = Logger.getLogger(getClass().getName());
private Properties getHibernateProperties() {
// set hibernate properties
Properties props = new Properties();
props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
return props;
}
// need a helper method
// read environment property and convert to int
private int getIntProperty(String propName) {
String propVal = env.getProperty(propName);
// now convert to int
int intPropVal = Integer.parseInt(propVal);
return intPropVal;
}
@Primary
@Bean(name="dataSourceCDL")
public DataSource dataSourceCDL() {
// create connection pool
ComboPooledDataSource myDataSource = new ComboPooledDataSource();
// set the jdbc driver
try {
myDataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
} catch (PropertyVetoException exc) {
throw new RuntimeException(exc);
}
logger.info("jdbc.url=" + env.getProperty("jdbc.url"));
logger.info("jdbc.user.cdl=" + env.getProperty("jdbc.user.cdl"));
// set database connection props
myDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
myDataSource.setUser(env.getProperty("jdbc.user.cdl"));
myDataSource.setPassword(env.getProperty("jdbc.password.cdl"));
// set connection pool props
myDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
myDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
myDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));
myDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));
return myDataSource;
}
@Primary
@Bean(name="sessionFactoryCDL")
public LocalSessionFactoryBean sessionFactoryCDL() {
// create session factorys
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// set the properties
sessionFactory.setDataSource(dataSourceCDL());
sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan.cdl"));
sessionFactory.setHibernateProperties(getHibernateProperties());
return sessionFactory;
}
@Primary
@Bean(name="transactionManagerCDL")
@Autowired
public HibernateTransactionManager transactionManagerCDL(SessionFactory sessionFactory) {
// setup transaction manager based on session factory
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
}
db2Config.java
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("au.net.nbnco.mwoRoutingAPI")
@PropertySource({ "classpath:persistence-oracle-db.properties" })
public class RoutingAppConfigSAL implements WebMvcConfigurer {
@Autowired
private Environment env;
private Logger logger = Logger.getLogger(getClass().getName());
@Bean(name="dataSourceSAL")
public DataSource dataSourceSAL() {
// create connection pool
ComboPooledDataSource myDataSource = new ComboPooledDataSource();
// set the jdbc driver
try {
myDataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
} catch (PropertyVetoException exc) {
throw new RuntimeException(exc);
}
logger.info("jdbc.url=" + env.getProperty("jdbc.url"));
logger.info("jdbc.user.sal=" + env.getProperty("jdbc.user.sal"));
// set database connection props
myDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
myDataSource.setUser(env.getProperty("jdbc.user.sal"));
myDataSource.setPassword(env.getProperty("jdbc.password.sal"));
// set connection pool props
myDataSource.setInitialPoolSize(getIntProperty("connection.pool.initialPoolSize"));
myDataSource.setMinPoolSize(getIntProperty("connection.pool.minPoolSize"));
myDataSource.setMaxPoolSize(getIntProperty("connection.pool.maxPoolSize"));
myDataSource.setMaxIdleTime(getIntProperty("connection.pool.maxIdleTime"));
return myDataSource;
}
@Bean(name="sessionFactorySAL")
public LocalSessionFactoryBean sessionFactorySAL() {
// create session factorys
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
// set the properties
sessionFactory.setDataSource(dataSourceSAL());
sessionFactory.setPackagesToScan(env.getProperty("hibernate.packagesToScan.sal"));
sessionFactory.setHibernateProperties(getHibernateProperties());
return sessionFactory;
}
@Bean(name="transactionManagerSAL")
@Autowired
public HibernateTransactionManager transactionManagerSAL(SessionFactory sessionFactory) {
// setup transaction manager based on session factory
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
private Properties getHibernateProperties() {
// set hibernate properties
Properties props = new Properties();
props.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
props.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
return props;
}
// need a helper method
// read environment property and convert to int
private int getIntProperty(String propName) {
String propVal = env.getProperty(propName);
// now convert to int
int intPropVal = Integer.parseInt(propVal);
return intPropVal;
}
}
serviceClass.java
@服务
公共类RoutingServiceImpl实现了RoutingService {
@Autowired
private CISRoutingRegionDAO routingDAO;
@Autowired
private CISRoutingRegionCDLDAO routingCDLDAO;
@Override
@Transactional
public List<CISRoutingRegion> getRegions() {
return routingDAO.getRegions();
}
@Override
@Transactional
public CISRoutingRegion getRegion(String theRegionId) {
return routingDAO.getRegion(theRegionId);
}
@Override
@Transactional
public List<CISRoutingRegionCDL> getRegionsCDL() {
// TODO Auto-generated method stub
return routingCDLDAO.getRegions();
}
@Override
@Transactional
public CISRoutingRegionCDL getRegionCDL(String theRegionId){
// TODO Auto-generated method stub
return routingCDLDAO.getRegion(theRegionId);
}
}