在同一交易中保存并查找

时间:2017-01-13 13:31:15

标签: spring-data

我使用Jboss JTS作为弹出数据的事务管理器。

当我尝试时,
1.保存实体
2.找到相同的实体

在同一事务中,存储库返回null(对于JPA和Cassandra)。

但是,如果它在不同的交易中,那么它可以正常工作 有关如何进行的任何建议?
这是预期的行为还是应该从交易区域检索?

transactionManager.beginTransaction();
repo.save(entity);
Entity entityFound = repo.findOne(id);
Assert.assertTrue(entityFound != null); //fails

这是我的测试,

@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SDPTest {

@Autowired 
LaptopRepository repo;

@Autowired
TransactionManager txManager;

@Test
public void test() {
    try {

        final long id = 400;


        txManager.begin();
        LaptopEntity entity = TestEntityFactory.generateTestLaptopEntity();
        entity.setId(id);

        repo.save(entity);

        LaptopEntity foundEntity = repo.findOne(id); // should find within transactional scope ? 
        Assert.notNull(foundEntity); // fails
        txManager.commit();

    } catch(Exception e) {
        e.printStackTrace();
    }
}

}

JBOSS TS事务bean配置,          

<bean id="jbossUserTransaction"
    class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple"/>


<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <ref bean="jbossTransactionManager" />
    </property>
    <property name="userTransaction">
        <ref bean="jbossUserTransaction" />
    </property>
</bean>

实体工厂配置ConfigurationOracle.java

@Configuration
@EnableTransactionManagement
@PropertySource("xdm-sdp-oracle.properties")
@EnableJpaRepositories(basePackages="${xdm.sdp.oracle.repo-package}", 
        entityManagerFactoryRef="entityManagerFactoryOrcl1")
public class ConfigurationOracle {
  @Autowired 
  Environment env;

  public DataSource buildOracleDataSource() throws SQLException {
    OracleDataSource dataSource = new OracleDataSource();

    dataSource.setUser(env.getProperty("jpa.oracle.username"));
    dataSource.setPassword(env.getProperty("jpa.oracle.password"));
    dataSource.setURL(env.getProperty("jpa.oracle.url"));

    dataSource.setImplicitCachingEnabled(true);
    dataSource.setFastConnectionFailoverEnabled(true);
    return dataSource;
  }

  @Bean(name="entityManagerFactoryOrcl1")
  LocalContainerEntityManagerFactoryBean entityManagerFactoryOrcl() throws SQLException {
    final DataSource oracleDataSource = buildOracleDataSource();
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setPersistenceUnitName(env.getProperty("jpa.oracle.persistence-name"));
    entityManagerFactoryBean.setDataSource(oracleDataSource);
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    entityManagerFactoryBean.setPackagesToScan(env.getProperty("xdm.sdp.oracle.entity-package"));

    PersistenceUnitPostProcessor processor = new PersistenceUnitPostProcessor() {
        public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo unitInfo) {
            unitInfo.setJtaDataSource(oracleDataSource);
        }
    };
    entityManagerFactoryBean.setPersistenceUnitPostProcessors(processor);

    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.hbm2ddl.auto", "create");

    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
  }
}

实体类

@Entity
public class LaptopEntity extends DataEntity implements Persistable<String> {


private String name;
private String macID;

@Id
private String machineID;
private long memoryCapacity;
private long storageCapacity;

private double powerDraw;
private double batteryStandbyTime;

private boolean isShared;
private boolean webcamPresent;
private boolean fingerprintSensorPresent;

private boolean isNewEntity;



public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getMacID() {
    return macID;
}
public void setMacID(String macID) {
    this.macID = macID;
}
public boolean isShared() {
    return isShared;
}
public void setShared(boolean isShared) {
    this.isShared = isShared;
}
public long getMemoryCapacity() {
    return memoryCapacity;
}
public void setMemoryCapacity(long memoryCapacity) {
    this.memoryCapacity = memoryCapacity;
}
public long getStorageCapacity() {
    return storageCapacity;
}
public void setStorageCapacity(long storageCapacity) {
    this.storageCapacity = storageCapacity;
}


public double getPowerDraw() {
    return powerDraw;
}
public void setPowerDraw(double powerDraw) {
    this.powerDraw = powerDraw;
}
public double getBatteryStandbyTime() {
    return batteryStandbyTime;
}
public void setBatteryStandbyTime(double batteryStandbyTime) {
    this.batteryStandbyTime = batteryStandbyTime;
}
public boolean isWebcamPresent() {
    return webcamPresent;
}
public void setWebcamPresent(boolean webcamPresent) {
    this.webcamPresent = webcamPresent;
}
public boolean isFingerprintSensorPresent() {
    return fingerprintSensorPresent;
}
public void setFingerprintSensorPresent(boolean fingerprintSensorPresent) {
    this.fingerprintSensorPresent = fingerprintSensorPresent;
}


public int compareTo(LaptopEntity machine) {
    return this.machineID.compareTo(machine.getMachineID());
}
public Class<? extends CrudRepository> getRepoClass() {
    return LaptopRepository.class;
}
public String getMachineID() {
    return machineID;
}
public void setMachineID(String machineID) {
    this.machineID = machineID;
}
public String getId() {
    // TODO Auto-generated method stub
    return machineID;
}
public boolean isNew() {
    // TODO Auto-generated method stub
    return isNewEntity;
}
public boolean isNewEntity() {
    return isNewEntity;
}
public void setNewEntity(boolean isNewEntity) {
    this.isNewEntity = isNewEntity;
}




}

存储库,

public interface LaptopRepository extends JpaRepository<LaptopEntity, Long> { 

}

最后,弹簧配置文件SpringConfig.java

@Configuration
@ImportResource({"classpath:jboss-springContext.xml","classpath:property-placeholder.xml"})
@Import({ConfigurationOracle.class})
@ComponentScan
public class SpringConfig {}

0 个答案:

没有答案