我用数据源路由创建了一个 spring-boot 应用程序。我已经使用 javax.persistence.EntityManager 使用存储过程从数据库中检索数据。重新启动sql server之后,EntityManager无法使用存储过程从数据库检索数据。 我同时使用了属性 TestOnBorrow 和 ValidationQuery 。
RoutingDataSourceCode
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_notification" />
}
使用存储过程从数据库获取数据。
public class MessagingChildDataSourceConfig{
private static final Logger LOG = LoggerFactory.getLogger(MessagingChildDataSourceConfig.class);
@Autowired
protected MessagingDataSourceUtil util;
@Autowired
protected JPAProperties jpaProperties;
@Bean(name = "messageDatasource")
@PostConstruct
public DataSource messageDatasource() throws SQLException {
Map<Object, Object> datasourceMap = new HashMap<>();
DataSource msgChildDataSources = DataSourceBuilder.create()
.url("URL")
.username("USER")
.password("PASS")
.build();
datasourceMap.put("testDataSOurce", msgChildDataSources);
MessagingRoutingDataSource datasource = new MessagingRoutingDataSource();
DataSource msgDefaultDataSource = util.defaultDataSource();
util.setTypeSpecificProperties(msgDefaultDataSource);
datasource.setDefaultTargetDataSource(msgDefaultDataSource);
datasource.setTargetDataSources(datasourceMap);
datasource.afterPropertiesSet();
return datasource;
}
@Bean(name = "messageEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean messageEntityManagerFactory(EntityManagerFactoryBuilder builder,
@Qualifier("messageDatasource") DataSource childDatasource) {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factory = builder.dataSource(childDatasource)
.packages(getPackages()) // for entity package
.persistenceUnit("messagesdbTest").properties(CommonUtil.getJPAProperties(jpaProperties)).build();
factory.setJpaVendorAdapter(vendorAdapter);
return factory;
}
@Bean(name = "messageTransactionManager")
public PlatformTransactionManager messageTransactionManager(
@Qualifier("messageEntityManagerFactory") EntityManagerFactory childEntityManagerFactory) {
return new JpaTransactionManager(childEntityManagerFactory);
}
@Bean(name = "channelEntityManager")
public EntityManager entityManager(@Qualifier("messageEntityManagerFactory") LocalContainerEntityManagerFactoryBean factory){
return factory.getNativeEntityManagerFactory().createEntityManager();
}
public String[] getPackages(){
return new String[]{"com.asite.abc.data.entity"};
}
}
属性:
public class ChannelService {
@Qualifier("channelEntityManager")
@Autowired
private EntityManager entityManager;
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true, isolation = Isolation.READ_UNCOMMITTED, transactionManager = MessagingStringPool.CHANNEL_TRANSACTION_MANAGER)
public void getUserAccessibleDCs(Long channelId, Long projectId) {
StoredProcedureQuery query = this.entityManager.createNamedStoredProcedureQuery("channelUserAccessibleDCs");
this.entityManager.setFlushMode(FlushModeType.AUTO);
query.setParameter("channel_id", channelId);
query.execute(); //**Error from this line**
}
使用EntityManager执行存储过程查询时,出现ERROR ,重新启动sql server之后。错误日志
spring.datasource.password=Pass
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.testOnBorrow=true
spring.datasource.username=Username
spring.datasource.validationQuery=SELECT 1
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.database=default
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true