Can Jpa transaction and jdbc transaction use same datasource - jpa

I have a code which uses Hibernate Jpa transaction and an independent scheduler is written which uses jdbctransaction . My application context xml has both org.springframework.orm.jpa.JpaTransactionManager and org.springframework.jdbc.datasource.DataSourceTransactionManager beans. Can both the transaction managers use same datasource defined in xml. Or do I need to create a new datasource bean for jdbc transaction

Related

JPA EntityManagerFactory with AbstractRoutingDataSource containing multiple DB vendors?

So I have a perfectly good working example of using AbstractRoutingDataSource and JdbcTemplate with Oracle / Sybase & MsSql databases in the same running spring boot application. I use AOP and a custom annotation on the method so that it sets the data source name on the thread and then the AbstractDataSource hands the correct data source to JdbcTemplate when you run a query.
Now the issue I am facing, is how I go about configuring the hibernate dialects when configuring the EntityManagerFactoryBuilder, as these are obviously different and based on the underlying active data sources (can differ between environments). The code you would use to configure the EntityManagerFactory if all data sources were the same would be as follows.
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(dataSource)
.packages("<the associated entity package name>")
.build();
But when I start the spring boot application, I get the error below
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Anyone know a workaround for this or is it not possible to have the same JPA Entities and CrudRepository instances spread across multiple datasources with different vendors?

Possibility to use JPA without EJB inside WebSphere module

I have a WebSphere ESB 7.5 hosting a web service (inside a mediation module).
The data from the web service should be stored to a DB. DB access should be performed via JPA.
I would like to utilize JPA with WebSphere's container-managed transactions (so JPA just replaces plain SQL calls and that's it). I don't want to generate an EJB from this tutorial. This seems unnecessary for my case.
Is it possible? Any code example?
There is nothing in the JPA specification that mandates it be used in an application container or via EJB's. But, as to your second condition, container managed transactions are defined at the level of the container, and more specifically, they are only valid for use in entity beans. So, your options are to use:
JPA + non-entity beans + user transactions
JPA + entity beans + container managed transaction.

Toplink and CMT message driven bean

I am trying to integrate Toplink with CMT Message driven bean. MY MDB is CMT. When I try to use unitofwork commit it is erroring out saying a global transaction is present so can not do local commit. After researching toplink they suggested following things. use external connection pool and use getactiveUnit of work to commit. We are using oracle 10.1.3 container for connection pooling and external transaction controller (OC4J transaction controller). When I changed to getActiveUnitWork().commit, I get null pointer because of null active unit of work. My understanding is container starts a transaction when on message of MDB gets executed. So toplink getactive unit of work should associate a unit of work with external transaction. Toplink GetActive unit of work method should return null only when there is no external transaction is present. I am not sure how to solve this issue or what is wrong. I appreciate any help on this.
Thanks.
TZ
Ensure you have set your ExternalTransactionController on your session correctly, and that there is a JTA transaction active.

jta and non jta in pe

If I mention both jta and non-jta datasource in persistence.xml, how will the provider identify what and when to use? Is there a way to enforce usage of non-jta datasource in certain scenario? I am using IBM supported OpenJPA.
Some providers allow to declare both a jta-datasource and a non-jta-datasource and use the later for optimized reading through non-JTA connections (i.e. that won't be associated to an ongoing JTA transaction). How this works and if OpenJPA supports this?
Difference between a "jta-datasource" and a " resource-local " datasource?
Probably what you want is to cofigure separate persistence units for the data sources & then injecting accordingly.
<persistence-unit name="JTA_DS" transaction-type="JTA">
<jta-data-source>java:JTA_DS</jta-data-source>
</persistence-unit>
<persistence-unit name="NON_JTA_DS" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:NON_JTA_DS</non-jta-data-source>
</persistence-unit>
Now you can create EnityManager for respective persistence units.
#PersistenceContext(unitName="JTA_DS")
private EntityManager _JTAManager;
#PersistenceContext(unitName="NON_JTA_DS")
private EntityManager _NonJTAManager;
Else you can build EntityManagerFactory manually as required using appropriate datasource. You can refer this link for more details.
It is difficult to understand why you mention both jta and non-jta datasource in persistence.xml. You don't need to declare both and you may need to declare one, jta or non-jta datasource.If you uses jta datasource, you will configure this in your application server and also declare in your persistence.xml.If you can't use jta datasource, you will configure JDBC connection in your persistence.xml.

Problem with EJB + POJO Helpers + EntitiyManager

I'm working with EJBs...I do the following and I don't know why the injected EntityManager is not working as one might expect.
EJB1 calls a method on EJB2 that writes to the DB.
when EJB2 returns EJB1 sends a message to a MDB.
MDB calls EJB3 that reads the DB and does some work.
My problem is that the EntityManager injected in all 3 EJBs with #PersistenceContext is not working properly. Calling persist() in EJB2 is not being reflected on the EntityManager injected in EJB3.
What might be wrong?
Hope I made my problem clear enough.
now working with Container managed transactions.
My problem is that the EntityManager injected in all 3 EJBs with #PersistenceContext is not working properly. Calling persist() in EJB2 is not being reflected on the EntityManager injected in EJB3.
In a Java EE environment, the common case is to use a Transaction-Scoped Container-Managed entity manager. And with such an entity manager, the persistence context propagates as the JTA transaction propagates.
In your case, I suspect you're using a REQUIRES_NEW transaction attribute for the method of EJB3. So:
when invoking EJB3#bar(), the container will suspend the transaction started for EJB2#foo() and start a new transaction
when invoking the entity manager from EJB3#bar(), a new persistence context will be created.
since the transaction started for EJB2#foo() has not yet committed, changes aren't "visible" to the new persistence context.
PS: Are you really creating new threads? If yes, little reminder: this is forbidden by the EJB spec.