同事们,你能帮我做@Transactional
注释吗?我的目标是将数据库事务管理传递给Spring,并在当前情况下将数据保存在数据库中。
我在DAO类中有两种方法:
@Component
public class OdValuesDAO
{
static final Logger LOG =
Logger.getLogger(OdValuesDAO.class.getName());
@Autowired
// @PersistenceContext(unitName = "PersistenceUnit")
@Qualifier("emR")
private EntityManager em;
public void addOdValue (OdValuesEntity odValuesEntity) {
LOG.info(odValuesEntity.toString());
//EntityTransaction tx = em.getTransaction(); Data will be saved DB if uncomment this code.
///tx.begin();
//LOG.info(tx.isActive());
em.persist(odValuesEntity);
///tx.commit();
}
public List<OdValuesEntity> getShare (OdValuesEntity odValuesEntity) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<OdValuesEntity> criteriaQuery = cb.createQuery(OdValuesEntity.class);
Root<OdValuesEntity> entityRoot = criteriaQuery.from(OdValuesEntity.class);
Predicate criteria = cb.conjunction();
if (odValuesEntity.getId() != null) .........
criteriaQuery.where(criteria);
List<OdValuesEntity> result = em.createQuery(criteriaQuery).getResultList();
return result;
}
服务方式:
@Component
public class OdValuesService {
static final Logger LOG = Logger.getLogger(OdValuesService.class.getName());
@Autowired
private OdValuesDAO odValuesDAO;
/**
* Read data from table.
* @param odValuesEntity
* @return
*/
@Transactional (readOnly = true, value = "txtxManagerR")
public List<OdValuesEntity> getShare (OdValuesEntity odValuesEntity) {
odValuesDAO.getShare(odValuesEntity);
return odValuesDAO.getShare(odValuesEntity);
}
/** Add record to the table
*/
@Transactional (value = "txtxManagerR") /*I have two transaction managers for two different DataSources/
public void addOdValue (OdValuesEntity odValuesEntity) {
odValuesDAO.addOdValue(odValuesEntity);
}
}
我有一个测试,调用这两种方法。我可以使用getShare
方法从表中读取数据。但是调用方法addOdValue
后没有保存数据。这是我的问题。当我取消注释addOdValue
中与事务相关的代码时,一切正常。正如我所理解的,在这种情况下,Spring事务管理器不起作用。
如何使用spring@Transactional注释更正addOdValue
方法或spring配置文件,以将数据保存在数据库中(打开并提交事务)?
我的Spring配置(上下文)包含下一个要使用DB的bean:
@Configuration
@EnableBatchProcessing
@EnableTransactionManagement
@ComponentScan
public class AppConfig {
@Bean
public BasicDataSource specRDataSource() {
BasicDataSource specRDataSource = new BasicDataSource();
specRDataSource.setDriverClassName("org.firebirdsql.jdbc.FBDriver");
specRDataSource.setUrl(specRDbUrl);
specRDataSource.setUsername(specRDbUser);
specRDataSource.setPassword(specRDbPassword);
specRDataSource.setMaxIdle(30);
specRDataSource.setMaxWaitMillis(10000);
specRDataSource.setValidationQuery("select 1 from rdb$database");
specRDataSource.setTestOnBorrow(false);
specRDataSource.setTestWhileIdle(true);
specRDataSource.setDefaultAutoCommit(true);
return specRDataSource;
}
@Bean
public BasicDataSource bortDataSource() {
BasicDataSource bortDataSource = new BasicDataSource();
bortDataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
bortDataSource.setUrl(bortDbUrl);
bortDataSource.setUsername(bortDbUser);
bortDataSource.setPassword(bortDbPassword);
bortDataSource.setMaxIdle(2);
bortDataSource.setMaxWaitMillis(10000);
bortDataSource.setValidationQuery("select 1");
bortDataSource.setTestOnBorrow(true);
bortDataSource.setTestWhileIdle(true);
bortDataSource.setDefaultAutoCommit(true);
return bortDataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory (BasicDataSource bortDataSource) {
LocalContainerEntityManagerFactoryBean localConnectionFactoryBean = new LocalContainerEntityManagerFactoryBean();
localConnectionFactoryBean.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
localConnectionFactoryBean.setDataSource(bortDataSource);
localConnectionFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
return localConnectionFactoryBean;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactorySpec (BasicDataSource specRDataSource) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
emf.setDataSource(specRDataSource);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("hibernate.dialect", "org.hibernate.dialect.FirebirdDialect");
emf.setJpaPropertyMap(properties);
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setShowSql(true);
emf.setJpaVendorAdapter(hibernateJpaVendorAdapter);
return emf;
}
@Bean
public EntityManager emR (@Qualifier("entityManagerFactorySpec") EntityManagerFactory entityManagerFactorySpec) {
return entityManagerFactorySpec.createEntityManager();
}
@Bean
public EntityManager embort (@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory ) {
return entityManagerFactory.createEntityManager();
}
@Bean
public JpaTransactionManager txtxManagerR (@Qualifier("entityManagerFactorySpec") EntityManagerFactory entityManagerFactorySpec) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactorySpec);
return txManager;
}
@Bean
public JpaTransactionManager txManagerbort (@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
我的坚持。xml看起来像(如果需要):
<persistence-unit name="PersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.acap.app.JpaEntities.OdDocsEntity</class>
<class>com.acap.app.JpaEntities.OdValuesEntity</class>
.......
<class>com.acap.app.JpaEntities.OdOSharesEntity</class>
<properties>
<property name="hibernate.connection.url"
value="connection string"/>
<property name="hibernate.connection.driver_class" value="org.firebirdsql.jdbc.FBDriver"/>
<property name="hibernate.connection.username"/>
<property name="hibernate.connection.password"/>
<property name = "hibernate.show_sql" value = "false" />
<property name = "hibernate.format_sql" value = "false" />
</properties>
</persistence-unit>
堆栈跟踪未显示任何错误。谢谢你的帮助。
更新测试运行:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class OdValuesServiceTest {
static final Logger LOG = Logger.getLogger(OdValuesServiceTest.class.getName());
@Autowired
OdValuesService odValuesService;
@Autowired
DBCommonsService dbCommonsService;
@Commit
@Test
public void addOdValue() throws Exception {
OdValuesEntity odValuesEntity = new OdValuesEntity();
odValuesEntity.setId(dbCommonsService.getNextDocOD("OD_VALUES_ID_GEN"));
odValuesEntity.setSysname("Name" + DataGenerator.getRandomISIN());
odValuesEntity.setName("Name");
odValuesEntity.setIsIn((short) 1);
odValuesEntity.setvType(2);
odValuesEntity.setMfu((short) 0);
odValuesEntity.setIsin("AU000A0JP922");
odValuesEntity.setCfi("");
odValuesService.addOdValue(odValuesEntity);
}
}
从Spring留档:
15.2.3交易管理
[..]默认情况下,框架将为每个测试创建并回滚一个事务。[..]
因此,如果您的“问题”是,在使用@Transactional
时,在测试中没有数据写入数据库,那么这完全没有问题,Spring默认值就是这样工作的。如果那不是你想要的...
如果您希望提交事务 — 不寻常,但在需要特定测试填充或修改数据库时偶尔有用 — 可以通过@commit注释指示TestContext框架使事务提交,而不是回滚。
我是JUnit新手,尝试在我的遗留代码库中实现它<当我测试hibernateEntity时,我应该断言什么。save()
这段代码导出sql数据到csv文件,数据是出来了,显示在网页上,不保存成csv文件,请教一下什么原因?
样板 正常工作,但不包括。它给了我一个错误,说: 无法读取未定义的属性包含 剧本 这就是我的axios api调用的样子 混血儿 当我在created()钩子中控制台log responseData时,我得到一个空代理。 当我将它登录到挂载的钩子中时,我会得到一个具有正确数据的代理对象,但是如果我尝试从挂载的钩子调用API mixin,我仍然会得到与以前相同的错误,我的其余页面会中断。 浏览器中的
问题内容: 我是AngularJs的新手,所以这可能微不足道。是否有内置的AngularJ 来检测表单中未保存的数据。如果没有,那么如何去写一个。任何指针将不胜感激。 html代码是 我的角度js控制器代码是 我正在尝试编写指令以检测未保存的数据,并且我猜想它会写在上述控制器中。 问题答案: AngularJS设置CSS类和任何输入栏你已经使用在NG- 模型,您的FormController拥有的
为什么我的值不能进入数据库? 如果需要更多的代码,请告诉我。 警告: 查询连接中的异常。php第713行:SQLSTATE[23000]:完整性约束冲突:1048列“name”不能为空(SQL:insert-into(,,,,)值(,,2016-06-19 21:44:05,2016-06-19 21:44:05)) 控制器: HTML页面: