如何使用Hibernate,HikariCP和persistence.xml实现Spring的@Transactional注解 [英] How to implement Spring's @Transactional Annotation using Hibernate, HikariCP and persistence.xml
问题描述
我需要实现Spring的@Transactional注解,但我无法做到这一点。
过去3周我尝试了很多方法,但他们都没有工作。
我也需要使用EntityManager。为了测试是否Spring的是工作,我尝试了EntityManager使用@PersistenceContext注入(我也使用@PersistenceUnit尝试和/与EntityManagerFactory的),但我总是得到NullPointerException异常。
@PersistenceContext(unitName =sistema)
受保护的EntityManager entityManager;
基本上我需要知道如何让Spring的注释工作以及如何使用这些注释来实现事务管理器技术:
$ b
persitence.xml
<?xml version =1.0encoding =UTF-8?>
< persistence xmlns =http://java.sun.com/xml/ns/persistence
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
version = 2.0 >
< persistence-unit name =sistematransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> com.sis.vo.Person< / class>
<属性>
< property name =hibernate.connection.driver_classvalue =com.mysql.jdbc.Driver/>
< property name =hibernate.connection.autocommitvalue =false/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQL5Dialect/>
< property name =hibernate.enable_lazy_load_no_transvalue =false/>
< property name =hibernate.show_sqlvalue =false/>
< property name =hibernate.format_sqlvalue =false/>
<! - 我需要那些吗? - >
< property name =hibernate.connection.urlvalue =jdbc:mysql:// localhost / myDatabase?autoReconnect = true& amp; amp; useSSL = false/>
< property name =hibernate.connection.usernamevalue =myUser/>
< property name =hibernate.connection.passwordvalue =myPass/>
< / properties>
< / persistence-unit>
< /余辉>
spring.xml
<?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:p =http://www.springframework.org/schema/p
xmlns:context =http://www.springframework.org/schema/context
xmlns :mvc =http://www.springframework.org/schema/mvc
xmlns:jee =http://www.springframework.org/schema/jee
xmlns:tx = http://www.springframework.org/schema/tx
xsi:schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework.org/ schema / beans / spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context- 4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http ://www.springframework .org / schema / jee
http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd\">
< context:annotation-config />
< mvc:annotation-driven />
< bean id =hikariConfigclass =com.zaxxer.hikari.HikariConfig>
< property name =poolNamevalue =sisHikariCP/>
< property name =connectionTestQueryvalue =SELECT 1/>
< property name =dataSourceClassNamevalue =com.mysql.jdbc.jdbc2.optional.MysqlDataSource/>
< property name =minimumIdlevalue =3/>
< property name =maximumPoolSizevalue =100/>
< property name =idleTimeoutvalue =740000/>
< property name =maxLifetimevalue =1740000/>
< property name =leakDetectionThresholdvalue =30000/>
< property name =dataSourceProperties>
<道具>
< prop key =url> jdbc:mysql:// localhost / myDatabase?autoReconnect = true& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;
< prop key =user> myUser< / prop>
< prop key =password> myPass< / prop>
< prop key =prepStmtCacheSize> 350< / prop>
< prop key =prepStmtCacheSqlLimit> 2048< / prop>
< prop key =cachePrepStmts> true< / prop>
< prop key =useServerPrepStmts> true< / prop>
< prop key =useLocalSessionState> true< / prop>
< prop key =useLocalTransactionState> true< / prop>
< prop key =rewriteBatchedStatements> true< / prop>
< prop key =cacheResultSetMetadata> true< / prop>
< prop key =cacheResultSetMetadata> true< / prop>
< prop key =cacheServerConfiguration> true< / prop>
< prop key =elideSetAutoCommits> true< / prop>
< prop key =maintainTimeStats> false< / prop>
< /道具>
< / property>
< / bean>
< bean id =dataSourceclass =com.zaxxer.hikari.HikariDataSourcedestroy-method =close>
< constructor-arg ref =hikariConfig/>
< / bean>
< bean id =myJpaVendorAdapterclass =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapterref =myJpaVendorAdapter/>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
< / beans>
web.xml
<?xml version =1.0encoding =UTF-8?>
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns =http://java.sun.com/xml/ns / javaeexmlns:web =http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
xsi:schemaLocation =http://java.sun.com/xml / ns / javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
id =WebApp_IDversion =3.0>
< display-name> sis< / display-name>
< context-param>
< param-name> javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL< / param-name>
< param-value> true< /参数值>
< / context-param>
< welcome-file-list>
< welcome-file> index.jsf< / welcome-file>
< / welcome-file-list>
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> *。jsf< / url-pattern>
< / servlet-mapping>
<! - - SPRING - >
< servlet>
< servlet-name> Spring Servlet< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< init-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/spring.xml
< /参数值>
< / init-param>
1< / load-on-startup>
< / servlet>
< / web-app>
DAO
@Repository
public class GenericListDAO< E> {
@PersistenceContext(unitName =sistema)
受保护的EntityManager entityManager;
public EntityManager getEntityManager(){
return entityManager;
$ b $ public GenericListDAO(){
}
//其余代码(持久,查找等)
}
版本:
谢谢!
这就是我用来配置我的事务管理器
< bean id =myDatasourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close>
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>
< bean id =myJpaVendorAdapterclass =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =myDatasource/>
< property name =jpaVendorAdapterref =myJpaVendorAdapter/>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
entityManagerFactory
bean将使用<$ c
$ b
更新
获取您的$ c $> persistence.xml 来创建持久性单元。像这样的持久化上下文
@PersistenceContext(unitName =sistema)
受保护的EntityManager em;
也确保使用持久化上下文的类使用 @注释存储库
更新
您可以将其添加到 web.xml
<! - 所有Servlet共享的Root Spring容器的定义
和过滤器 - >
< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
classpath:/WEB-INF/spring-root-context.xml
< / param-value>
< / context-param>
<! - 创建所有Servlet和过滤器共享的Spring容器 - >
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
现在创建一个新文件 spring-root-context.xml
在 spring.xml
旁边,将所有DB和事务相关的东西移动到 spring-root-context.xml $ c $所以
spring.xml
将成为你的servlet上下文, spring-root -context.xml
将作为根上下文。
与控制器相关的任何内容都将转到 spring.xml code>所有其他的bean将会到
spring-root-context.xml
还会添加< context:component-scan base-package =package.for.genericdao/>
< spring-root-context.xml
,并将package.for.genericdao替换为您的基本软件包名称。
I need to implement Spring's @Transactional annotation but I'm not being able to do so.
I had tried a lot of methods in the past 3 weeks but none of them worked.
I'm also need to use EntityManager. To test if Spring's was working, I tried to inject the EntityManager using @PersistenceContext (I also tried using @PersistenceUnit and/with EntityManagerFactory) but always I got nullPointerException.
@PersistenceContext(unitName = "sistema")
protected EntityManager entityManager;
Basically I need to know how to make spring's annotations to work and how to implement a Transaction manager using those technologies:
persitence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="sistema" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.sis.vo.Person</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.autocommit" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.enable_lazy_load_no_trans" value="false"/>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false"/>
<!-- do I need those? -->
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/myDatabase?autoReconnect=true&useSSL=false" />
<property name="hibernate.connection.username" value="myUser" />
<property name="hibernate.connection.password" value="myPass" />
</properties>
</persistence-unit>
</persistence>
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:annotation-config />
<mvc:annotation-driven />
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="sisHikariCP" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
<property name="minimumIdle" value="3" />
<property name="maximumPoolSize" value="100" />
<property name="idleTimeout" value="740000" />
<property name="maxLifetime" value="1740000" />
<property name="leakDetectionThreshold" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">jdbc:mysql://localhost/myDatabase?autoReconnect=true&useSSL=false</prop>
<prop key="user">myUser</prop>
<prop key="password">myPass</prop>
<prop key="prepStmtCacheSize">350</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
<prop key="cachePrepStmts">true</prop>
<prop key="useServerPrepStmts">true</prop>
<prop key="useLocalSessionState">true</prop>
<prop key="useLocalTransactionState">true</prop>
<prop key="rewriteBatchedStatements">true</prop>
<prop key="cacheResultSetMetadata">true</prop>
<prop key="cacheResultSetMetadata">true</prop>
<prop key="cacheServerConfiguration">true</prop>
<prop key="elideSetAutoCommits">true</prop>
<prop key="maintainTimeStats">false</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="myJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="myJpaVendorAdapter" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>sis</display-name>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<!-- SPRING -->
<servlet>
<servlet-name>Spring Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
DAO
@Repository
public class GenericListDAO<E> {
@PersistenceContext(unitName = "sistema")
protected EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public GenericListDAO() {
}
//rest of the code (persist, find, etc)
}
Versions:
- Spring Version: 4.3.12
- Hibernate Version: 5.1.10.Final
- HikariCP Version: 2.7.3
- JDK Version: 1.8.0_121
- Tomcat version: 8.5.23
Thank you!
This is what i use to configure my transaction manager
<bean id="myDatasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="myJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDatasource" />
<property name="jpaVendorAdapter" ref="myJpaVendorAdapter" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
entityManagerFactory
bean will use your persistence.xml
from classpath to create the persistance unit.
update get your persistence context like this
@PersistenceContext(unitName="sistema")
protected EntityManager em;
also make sure the class that is using the persistence context is annotated with @Repository
update
can you add this to your web.xml
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/WEB-INF/spring-root-context.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
now create a new file spring-root-context.xml
next to spring.xml
and move all the DB and transaction related stuff to spring-root-context.xml
.
So spring.xml
will be your servlet context and spring-root-context.xml
will be your root context.
anything related to controllers will go to spring.xml
all other beans will go to spring-root-context.xml
also add <context:component-scan base-package="package.for.genericdao" />
to both spring.xml
and spring-root-context.xml
and replace package.for.genericdao with your base package name.
这篇关于如何使用Hibernate,HikariCP和persistence.xml实现Spring的@Transactional注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!