I have two transactions managers defined in applicationContext, referencing two completely different sessionFactories. I am attempting to use annotations with one of the transactionmanagers however it seems to be applying all annotations to both transaction managers.
The new db I am migrating to has <property name="hbm2ddl.auto">create</property> and the other which I am migrating from has <property name="hbm2ddl.auto">validate</property>
I have annotated my new domain objects with #Entity hibernate is attempting to validate these classes on the legacy database, which I thought I had set up to ignore annotations (by not specifying annotations). Here are the relevant snippets of appContext:
<tx:annotation-driven transaction-manager="transactionManager1" />
<context:property-placeholder location="file:${catalina.home}/conf/database.properties" ignore-unresolvable="true"/>
<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource1" p:configLocation="WEB-INF/classes/hibernate.cfg.xml" p:packagesToScan="com.mycompany"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:driverClass="${app.driverClassName}" p:jdbcUrl="${app.url}"
p:user="${app.username}" p:password="${app.password}"
<!-- Declare a transaction manager-->
<bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory1" />
and the legacy db in same appContext :
<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource2" p:configLocation="WEB-INF/classes/hibernateTraveller.cfg.xml" p:packagesToScan="com.mycompany"/>
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${app2.driverClassName}" p:jdbcUrl="${app2.url}"
p:user="${app2.username}" p:password="${app2.password}"
<!-- Declare a transaction manager-->
<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactoryTraveller">
<qualifier value="legacyDB"/>
</bean>
any pointers ?
You should use LocalSessionFactoryBean instead of AnnotationSessionFactoryBean for your legacy database that does not use annotations. The whole purpose of the AnnotationSessionFactoryBean is to use annotations. If you don't want to use annotations, LocalSessionFactoryBean is the way to go.
Related
I am setting up an application with mybatis-spring, which should execute several sql-statements (mostly selects) and print the result to the console.
My applicationContext.xml looks like this:
<context:property-placeholder location="classpath:application.properties"/>
<!-- BEANS -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${spring.datasource.driverClassName}"/>
<property name="url" value="${spring.datasource.url}"/>
<property name="username" value="${spring.datasource.username}"/>
<property name="password" value="${spring.datasource.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:sql_mapper.xml"/>
</bean>
<bean id="organisationMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mapper.OrganisationMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
I have noticed that whenever I execute a sql-statement the session is created just for this statement and closes directly after execution.
Is there a way to execute multiple sql-statements in just one session, which closes itself not until all methods/statements are executed?
Thanks and Greetings.
It seems that transactions are not demarcated properly and the default behaviour (one call to mapper - one transaction - one mybatis session) is used.
In spring-mybatis the session bound to spring transaction. If transactions are not demarcated then a transaction is created (and hence mybatis SqlSession) for every call to mybatis mapper method.
In order to change this you need to properly configure spring so that:
transactions can be used at all
configure transaction boundaries that is what calls to the database should be executed in one transaction
There are many ways to configure transactions for details see documentation. I'll show here the simple way that uses xml configuration.
Firstly, add transaction manager to the spring configuration:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
Then you need to specify transactions boundaries. The simple way is to use declarative transaction management:
Add this to spring configuration
<tx:annotation-driven/>
And then use #Transactional annotation on methods that should be executed transactionally:
#Service
class SomeService {
#Autowired MyMapper mapper;
#Transactional
SomeResult someMethod () {
Piece1 piece1 = mapper.getSome();
Piece2 piece2 = mapper.getMore();
SomeResult result = SomeResult(piece1, piece2);
}
}
I have a service method that calls a DAO method. The service method is annotated as #Transactional, the DAO method is not. At runtime the following error occurs:
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
When I make the DAO method #Transactional as well, it works fine.
How can I fix this without touching the DAO? (Most methods are in a super-class DAO which I don't have access to.)
I'm using hibernate-core-3.6.8 and spring-orm-4.1.6.RELEASE
The DAOs are annotated as #Repository, the services as #Service
My applicationContext.xml looks like this (I just provided the important parts - let me know if you need more):
<bean id="contextApplicationContextProvider" class="at.spardat.deploysolution.process.context.ApplicationContextProvider"/>
<tx:annotation-driven/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<util:properties id="hibernateProperties" location="classpath:hibernate.properties"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
p:dataSource-ref="dataSource" p:hibernateProperties-ref="hibernateProperties">
<qualifier value="default"/>
<property name="mappingLocations">
<list>
<value>classpath:....hbm.xml</value>
</list>
</property>
<property name="lobHandler" ref="defaultLobHandler"/>
</bean>
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory"/>
<bean id="dataFieldMaxValueIncrementer" class="org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer"
p:dataSource-ref="dataSource" p:incrementerName="TB_OID_SEQUENCE" p:columnName="OID_VALUE">
<qualifier value="default"/>
</bean>
Can you change
hibernate.current_session_context_class=thread
in your hibernate properties file
Thanks
I'm a newbie with Java and I need to create a console application that is going to connect with 4 databases (access, vfp, mysql and sqlserver).
I started with hibernate.cfg.xml files and managed to configure them, one for each database. Then I realized that jpa was a better solution. So I changed all my hibernate files to a persistence.xml file with 4 persistence-unit.
The databases are working well, but to use them I have to create a lot of code. This is an example:
EntityManagerFactory dbPersistence =
Persistence.createEntityManagerFactory("oneOfMyDatabases");
EntityManager em = dbPersistence.createEntityManager();
Query query = em.createQuery("from ProductEntity").setMaxResults(10);
for (Object o : query.getResultList()) {
ProductEntity c = (ProductEntity) o;
System.out.println("Product " + c.getName());
}
cgPersistence.close();
I need to update one of the databases with data from the other databases.
It's a pain to create all the code like this, so I was thinking about creating repositories but I can't see how to create them with different entityManagers for each database.
I tried to inject the managers with google guice without success, but I couldn't handle how to close or where to close the persistence connection.
Finally, I've found Spring Data and it seems to be what I need, but I don't really understand how to use it.
I need some guide to get it working because I've read tons of tutorials and each of them appear to be different:
· Can I use the same persistence.xml or do I need another configuration file? I've seen that Spring Data has jpa compatibility but I'm not sure how it works.
· Is Spring Context the IOC Container of Spring Framework? Do I need it to work with Spring Data?
· What else do I need?
Thank you in advance
Each database is going to be represented by a different data source. For every data source you need a different session factory/entity manager factory.
If you want to save in more than one data source in a single transaction you then need XA transactions, therefore a Java EE or a stand-alone transaction manager, like Bitronix or Atomikos.
You have 4 different entity manager factories, you also need specific repositories for each of those:
<jpa:repositories base-package="your.company.project.repository.access" entity-manager-factory-ref="accessEntityManagerFactory"/>
<jpa:repositories base-package="your.company.project.repository.sqlserver" entity-manager-factory-ref="sqlserverEntityManagerFactory"/>
Then your application doesn't have to care which repository it uses.
The JpaTransactionManager requires one entityManagerFactory, but since you have 4 you may end up creating for of those, which I think it's undisirable.
It's better to switch to JTA instead:
<!-- 1. You define the Bitronix config ->
<bean id="btmConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices">
<property name="serverId" value="spring-btm"/>
<property name="warnAboutZeroResourceTransaction" value="true"/>
<property name="logPart1Filename" value="${btm.config.logpart1filename}"/>
<property name="logPart2Filename" value="${btm.config.logpart2filename}"/>
<property name="journal" value="${btm.config.journal:disk}"/>
</bean>
<!-- 2. You define all your data sources ->
<bean id="dataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init"
destroy-method="close">
<property name="className" value="${jdbc.driverClassName}"/>
<property name="uniqueName" value="dataSource"/>
<property name="minPoolSize" value="0"/>
<property name="maxPoolSize" value="5"/>
<property name="allowLocalTransactions" value="false"/>
<property name="driverProperties">
<props>
<prop key="user">${jdbc.username}</prop>
<prop key="password">${jdbc.password}</prop>
<prop key="url">${jdbc.url}</prop>
</props>
</property>
</bean>
<!-- 3. For each data source you create a new persistenceUnitManager and you give its own specific persistence.xml ->
<bean id="persistenceUnitManager" depends-on="transactionManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"/>
<property name="defaultDataSource" ref="dataSource"/>
<property name="dataSourceLookup">
<bean class="org.springframework.jdbc.datasource.lookup.BeanFactoryDataSourceLookup"/>
</property>
</bean>
<!-- JpaDialect must be configured for transactionManager to make JPA and JDBC share transactions -->
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<!-- 4. For each data source you create a new entityManagerFactory ->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<!-- 5. You have only one JTA transaction manager ->
<bean id="jtaTransactionManager" factory-method="getTransactionManager"
class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig, dataSource"
destroy-method="shutdown"/>
<!-- 6. You have only one Spring transaction manager abstraction layer ->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="jtaTransactionManager"/>
<property name="userTransaction" ref="jtaTransactionManager"/>
</bean>
If this proves too much work for your current problem, you can give a try to having 4 JPA transaction managers and see how it works.
If your schema is same across all the databases, then the best approach is to use hibernate,because hibernate provides portability between multiple databases. once you create the hbm files and pojo classes, the only thing you need to change is the dialect and your datasource.
The transactionmanager support can be provided by spring.
Example of spring configuration file:
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"ref="entityManagerFactory"/>
<property name="jpaDialect"ref="jpaDialect"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
....
</bean>
and the persistence.xml jpa file:
<persistence-unit name="EmployeeService">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
</persistence-unit>
As you can see the jpa provider-related information is set 3 times. In transaction manager bean, entity manager factory bean and in the persistence unit configuration:
<property name="jpaDialect"ref="jpaDialect"/>
...
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
...
<provider>org.hibernate.ejb.HibernatePersistence</provider>
But actually in my project I configured only the persistence unit with provider. And it worked.
So my question is what's the difference between provider, dialect and vendor options?
Must I set all of them or, I can skip some of them?
Can I set, for example as a vendor for EntityMangerFactory - Hibernate, as a dialect in transaction manager - Eclipse and as a provider in the persistence unit configuration - something else, TopLink, for example.
It's no clear to me. Please explain.
Will try to explain it to you line by line:
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
//Should ideally be
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
This bean defines the jpaDialect that you are going to use. JpaDialect is an interface encapsulates certain functionality that standard JPA 1.0 does not offer, such as access to the underlying JDBC Connection. This strategy is mainly intended for standalone usage of a JPA provider; most of its functionality is not relevant when running with JTA transactions.
Also allows for the provision of value-added methods for portable yet more capable EntityManager and EntityManagerFactory subinterfaces offered by Spring.
Since you have provided the class as class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>, this allows Spring to plug in vendor-specific behavior into Spring's EntityManagerFactory creators and it serves as single configuration point for all vendor-specific properties.It's a custom implementation of spring's own JpaVendorAdapter.
For the second bean where you have declared:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"ref="entityManagerFactory"/>
<property name="jpaDialect"ref="jpaDialect"/>
</bean>
You tell 'Spring' to configure a transactionManager whose properties are entityManagerFactory and jpaDialect. Since these properties have to specific to hibernate these are set according. The entityManagerFactory and jpaDialect are now set specifically to hibernate (or Vendor).
As for the third bean
<property name="jpaDialect"ref="jpaDialect"/>
...
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
...
<provider>org.hibernate.ejb.HibernatePersistence</provider>
The <provider> tells spring to use the hibernate provider and the class org.hibernate.ejb.HibernatePersistence is Hibernate EJB3 persistence provider implementation.
In short, you need to configure these in order to tell spring which ORM's functionality should be used.
The reason that your application worked with configuring just persistence and provider is because the vendor adapter is automatically passed the persistence provided i.e. HibernatePersistence via the getPersistenceProvider in JpaVendorAdapter.
Tinker around the documentation to understand how these classes are interlinked.
EDIT : As pointed out by #TheKojuEffect , the first bean should ideally be in the form of :
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
Thanks. Missed the vendorAdapter.
You can refer :
HibernateJpaDialect
HibernateVendorAdapter
HibernatePersistence
Hope it helps. :)
We have multiple war files packaged in an ear file. Some of the wars are using pure JDBC and we want to use hibernate for new wars (modules). We are using spring 2.5.6, hibernate 3.0 and jboss 4.2 server. We have following configuration for transactionmanager.
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:MyPool" />
</bean>
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="allowCustomIsolationLevels">
<value>true</value>
</property>
</bean>
It is working fine for us.
Now I want to configure hibernate transactionManger for new modules in a separate xml file.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:/hibernate.cfg.xml"/>
</bean>
Generally we configure hiberanate transaction mangager in following way.
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
But I would like to use same JtaTransactionManager reference defined for JDBC configuration. Please help me in configuring this.
You do not really need JTA if you are not using 2 phase commits. Just configure Hibernate trans mgr and it is capable of handling both hibernate and jdbc transactions. This link might also be helpful http://forum.springsource.org/showthread.php?t=69864