I have 2 DataSource connections and I am specifying which class each connection should use but I still get some exception:
this is my applicationContext.xml
<context:component-scan base-package="com.example.video"/>
<context:annotation-config/>
<tx:annotation-driven />
<jee:jndi-lookup id="VOD" jndi-name="java:comp/env/jdbc/VOD" />
<jee:jndi-lookup id="SB" jndi-name="java:comp/env/jdbc/SB" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="VOD" />
<property name="packagesToScan" value="com.example.video" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactorySB"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="SB" />
<property name="packagesToScan" value="com.example.service" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
</props>
</property>
</bean>
<bean id="transactionManagerSB"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactorySB"/>
</bean>
<jpa:repositories base-package="com.example.video"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"/>
<jpa:repositories base-package="com.example.service"
entity-manager-factory-ref="entityManagerFactorySB"
transaction-manager-ref="transactionManagerSB"/>
and here is my persistence.xml
<persistence-unit name="VOD" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.video.model.Product</class>
.
.
<class>com.example.video.model.Task</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="SB" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.example.service.model.ChargingInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>
And this is the exception:
Caused by: org.hibernate.HibernateException: Missing table: CHARGINGINFO
Have You checked MergingPersistenceUnitManager http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.misc.merging-persistence-units
Allows you to have multiple persistence providers combined to one.
Found the problem, just had to remove the line from applicationContext.xml
<
prop key="hibernate.hbm2ddl.auto">validate
from the bean declarations then it worked fine.
Related
persistence.xml
<persistence-unit name="user_per_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.example.User</class>
<class>com.example.Order</class>
<class>com.example.Package</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.jdbc.batch_size" value="100" />
<property name="hibernate.order_inserts" value="true" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
applicationContext.xml
<jpa:repositories base-package="com.example.user.repository" entity-manager-factory-ref="userEntityManagerFactory" transaction-manager-ref="userTransactionManager"/>
<bean id="userEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="user_per_unit" />
<property name="dataSource" ref="userDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<bean id="userTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="userEntityManagerFactory" />
</bean>
<bean id="userDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="initialSize" value="${size}" />
<property name="maxTotal" value="${maxtotal}" />
<property name="maxIdle" value="${maxidle}" />
<property name="minIdle" value="${minidle}" />
</bean>
Class
#Autowired
private ApplicationContext applicationContext;
private JpaTransactionManager userTransactionManager;
private EntityTransaction userTx;
userTransactionManager = (JpaTransactionManager) applicationContext.getBean("provTransactionManager");
userTx = provTransactionManager.getEntityManagerFactory().createEntityManager().getTransaction();
userTx.begin();
boolean isUpdateSuccess = updateViaUserRepository(...);
if(!isUpdateSuccess) {
userTx.rollback();
} else {
userTx.commit();
}
My code is reaching userTx.rollback(); but actual database is not getting rolledback. Is there any mistake in my persisence or applicationContext configurations.
Edit: All my tables have engine as InnoDb
First thing to look for: what is the table type that you are using?
For example when it comes to MySQL, MyISAM tables do not support transactions, which means you have to use another table type (e.g. InnoDB).
I use hibernate and i want to basic crud but i didn't configure persistence.xml and applicationContext.xml configuration file. I want to use EntityManager instead of Session.
This is my applicationContext.xml
<context:annotation-config />
<context:component-scan base-package="com.toyotaproject" />
<tx:annotation-driven />
<bean class = "org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/toyotadb" />
<property name="username" value="root" />
<property name="password" value="12345" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<context:component-scan base-package="com.toyotaproject.domain" />
<property name="persistenceXmlLocation" value="SpringJPA_PU"></property>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaAdapter" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</beans>
this is my persistence.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_1_0.xsd"
version="1.0">
<persistence-unit name="SpringJPA_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.toyotaproject.domain.Customer </class>
<class>com.toyotaproject.domain.Trip </class>
<class>com.toyotaproject.domain.Reservation </class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/toyotadb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="12345" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<!-- If you are using Hibernate's proprietary API, you'll need the hibernate.cfg.xml.
If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml. -->
<!-- SQL -->
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name ="hibernate.format_sql" value="true"/>
<property name ="hibernate.show_sql" value="true"/>
<!-- C3P0 -->
<property name="hibernate.c3p0.acquire_increment" value="2"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.timeout" value="180"/>
<property name="hibernate.c3p0.idle_test_period" value="100"/>
<property name="hbm2ddl.auto" value="update"/>
<!-- property name="eclipselink.logging.level" value="OFF" /-->
</properties>
</persistence-unit>
</persistence>
Where am i doing wrong ?
I'm developing an application with eclipseLink and Spring. For now I'm using h2 like database. My trouble is that eclipseLink is not able to create tables.
This is my configuration.
Persistence.xml:
<persistence>
<persistence-unit name="myUnit">
//some classes definition
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:file:C:\Users\user\Desktop\myFolder\test.db" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.composite-unit.member" value="true"/>
<property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.create-ddl-jdbc-file-name" value="create.sql"/>
</properties>
</persistence-unit>
</persistence>
spring-context.xml
<beans>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="emf">
<property name="persistenceUnitName" value="myUnit" />
</bean>
<bean id="eclipseLinkJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
</bean>
<tx:annotation-driven />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:jpaVendorAdapter-ref="eclipseLinkJpaVendorAdapter"
p:persistenceUnitName="myUnit">
<property name="jpaPropertyMap">
<map>
<entry key="eclipselink.cache.shared.default" value="false" />
<entry key="eclipselink.weaving" value="false" />
</map>
</property>
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
</beans>
Moreover i find the file create.sql empty!!!
Where am I doing wrong?
I'm trying to make an application with Spring 3, JPA 2 and Hibernate 3.
I have a problem when y persist an entity : nothing happen ! Data are not inserted in database, and not query is executed.
But when i'm using a request like query.getResultList() a select works correctly.
So I think my problem is only on a persist/update and on the transaction manager but i'm not really good with spring.
Can you help me please ?
Here are my configuration files :
My applicationContext.xml
<jee:jndi-lookup id="soireeentreamis_DS" jndi-name="jdbc/soireeentreamis" />
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="soireeentreamis_DS" />
<property name="dataSources">
<map>
<entry key="soireeentreamisDS" value-ref="soireeentreamis_DS" />
</map>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="soireeentreamisPU" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
</bean>
</property>
</bean>
<bean id="soireeentreamisTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven transaction-manager="soireeentreamisTransactionManager" />
<context:annotation-config />
</beans>
My persistence.xml
<persistence-unit name="soireeentreamisPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>soireeentreamisDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
My service
#Service
#Transactional("soireeentreamisTransactionManager")
public class UserServiceImpl implements UserService ...
My dao
#Repository
public class UserDaoImpl extends GenericDaoImpl<User, String> implements
UserDao {
#PersistenceContext(unitName = "soireeentreamisPU")
private EntityManager em;
public void persist(final User entity) {
em.persist(entity);
}
}
Can somebody help me please?
I find similar problem a while ago. In my case I needed to add line below to my dispacher-servlet.xml. So I need this in 2 places (applicationContex.xml and dispacher-servlet.xml)
<tx:annotation-driven />
And to clear something out, you didn't show your service methode that "store" object, but I believe that it's annotated with #Transactional - cause wihout this one you want create new transaction.
This happened to me recently. The problem is as you say a transactional problem, add the #Transactional annotation to all the methods that update the DB. Then add the CGLIB library to your calsspath or add it to your pom.xml if you are using Maven, this is necessary for spring make transactions. Even if I did it on a different way I hope it can help you. Here is my db.xml where I have all the data base related spring configuration.
<!-- Scans within the base package of the application for #Components to configure as beans -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="${db.dialect}" />
</bean>
</property>
</bean>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="idleConnectionTestPeriodInMinutes" value="1" />
<property name="idleMaxAgeInMinutes" value="4" />
<property name="maxConnectionsPerPartition" value="30" />
<property name="minConnectionsPerPartition" value="10" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="5" />
<property name="statementsCacheSize" value="100" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
And here is my persistence.xml
<persistence version="2.0"
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">
<persistence-unit name="pu-app" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
You need to add the model object into the persistence.xml file. Right below the provider element add
<class>com.your.domain.object.User</class>
can someone please show me how to change my XML to have hibernate to define the DataSource. I would like to see a good sample of it. Below is my xml file. can someone post what it would look like with hibernate defining the DataSource:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="org.uftwf" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- these are C3P0 properties -->
<property name="acquireIncrement" value="${database.acquireIncrement}" />
<property name="minPoolSize" value="${database.minPoolSize}" />
<property name="maxPoolSize" value="${database.maxPoolSize}" />
<property name="maxIdleTime" value="${database.maxIdleTime}" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="driverClass" value="${database.driver}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>org.uftwf.schoolvisit.model.VisitModel</value>
<value>org.uftwf.schoolvisit.model.NameID_lookupModel</value>
<value>org.uftwf.schoolvisit.model.School_lookupModel</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="format_sql">${format_sql}</prop>
<prop key="hibernate.c3p0.min_size">10</prop>
<prop key="hibernate.c3p0.max_size">25</prop>
<prop key="hibernate.c3p0.timeout">600</prop>
<prop key="hibernate.c3p0.max_statements">0</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.acquire_increment">5</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
This is a comment that someone made to me about my XML so I am trying to see what he is talking about:
You should simplify your own life by using those hibernate. properties to define the DataSource in order to avoid a maintenance nightmare down the road by mixing them. It will help to avoid confusion like this. And don't hardcode the values that you have data properties for.*
Should be like this:
src/main/resources/META-INF/persistence.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="show_sql" value="true"/>
<!-- Uncomment the following two properties for JBoss only -->
<!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
<!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
</properties>
</persistence-unit>
</persistence>
src/main/resources/META-INF/spring/applicationContext.xml:
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="3"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="10"/>
<property name="poolPreparedStatements" value="true"/>
<property name="maxOpenPreparedStatements" value="20"/>
</bean>