我有一个应用程序,它使用Spring Data JPA和Oracle数据库作为后端。 Entity类有一个id列,由数据库中的序列生成。
实体类是:
@Entity
@Table(name="ROLES")
public class AppRole {
@Id
@SequenceGenerator(name="SEQ_ROLES", sequenceName="SEQ_ROLES", allocationSize = 1)
@GeneratedValue(generator="SEQ_ROLES", strategy = GenerationType.SEQUENCE)
private Long id;
private String name;
private String description;
.. getters and setters
}
和spring数据jpa存储库接口是:
public interface AppRoleRepository extends JpaRepository<AppRole, Long>, JpaSpecificationExecutor<AppRole> {}
但是,每当我试图坚持AppRole的实体时,它都会抛出异常背景,但仍然可以正常工作。页面呈现良好,客户端没有任何反应。但是在服务器控制台中,它会输出这些行;
11:39:35.973 DEBUG JpaTransactionManager - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@640f233e]
11:39:35.977 DEBUG SQL - select SEQ_ROLES.nextval from dual
11:39:36.036 DEBUG SequenceGenerator - Sequence identifier generated: BasicHolder[java.lang.Long[36]]
11:39:36.036 DEBUG AbstractSaveEventListener - Generated identifier: 36, using strategy: org.hibernate.id.SequenceHiLoGenerator
11:39:36.036 DEBUG AbstractFlushingEventListener - Processing flush-time cascades
11:39:36.036 DEBUG AbstractFlushingEventListener - Dirty checking collections
11:39:36.037 DEBUG AbstractFlushingEventListener - Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
11:39:36.037 DEBUG AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
11:39:36.037 DEBUG EntityPrinter - Listing entities:
11:39:36.037 DEBUG EntityPrinter - AppRole{name=asdasd, description=asdasd, id=36}
11:39:36.051 DEBUG SQL - insert into ROLES (description, name, id) values (?, ?, ?)
11:39:36.091 DEBUG SqlExceptionHelper - could not log warnings
java.sql.SQLRecoverableException: Closed Statement
at oracle.jdbc.driver.OracleClosedStatement.getWarnings(OracleClosedStatement.java:6501) ~[ojdbc6-11.2.0.4.jar:11.2.0.3.0]
at oracle.jdbc.driver.OracleStatementWrapper.getWarnings(OracleStatementWrapper.java:1571) ~[ojdbc6-11.2.0.4.jar:11.2.0.3.0]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.handleAndClearWarnings(SqlExceptionHelper.java:291) ~[hibernate-core-4.2.18.Final.jar:4.2.18.Final]
我有这个例外。看来,从序列中获取id后,事务和连接都会关闭。然后它恢复并打开一个新连接并在这个新连接中执行此insert语句。
这是正常的,或者我误用或错误配置了什么错误?
非常感谢。
编辑:配置文件,服务实施和POM
持久性配置是:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("com.company.core.repository")
public class PersistenceConfig {
private @Autowired Constants constants;
@Autowired
private DataSource dataSource;
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean(name = "dataSource")
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource(
constants.getJdbcUrl(), constants.getJdbcName(),
constants.getJdbcPass());
ds.setDriverClassName(tfConstants.getJdbcDriver());
return ds;
}
@Bean
public AbstractEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean emFactory = new LocalContainerEntityManagerFactoryBean();
emFactory.setDataSource(dataSource);
emFactory.setPackagesToScan(constants.getApplicationPackageToScan());
emFactory.setPersistenceProviderClass(HibernatePersistence.class);
emFactory.setJpaProperties(getHibernateProperties());
emFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
return emFactory;
}
private Properties getHibernateProperties() {
Properties hp = new Properties();
hp.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
hp.setProperty("show_sql", "true");
hp.setProperty("hibernate.connection.release_mode", "after_transaction");
hp.setProperty("hibernate.current_session_factory_class", "thread");
return hp;
}
@Bean(name="transactionManager")
public AbstractPlatformTransactionManager getTransactionManager() {
JpaTransactionManager jpaTran = new JpaTransactionManager();
jpaTran.setEntityManagerFactory(entityManagerFactory);
return jpaTran;
}
}
服务实施是:
@Service
public class AppRoleService {
private @Resource AppRoleRepository roleRepo;
public void save(AppRole role) {
roleRepo.save(role);
}
}
和pom.xml:
...
<properties>
<java.version>1.7</java.version>
<spring.version>4.1.5.RELEASE</spring.version>
<hibernate.version>4.2.18.Final</hibernate.version>
<spring.data.jpa>1.7.2.RELEASE</spring.data.jpa>
</properties>
<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa}</version>
<exclusions>
<exclusion>
<artifactId>spring-asm</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validation.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
...