Jin Kwon
Jin Kwon

Reputation: 22027

Using EntityManager and DataSource together in a LocalBean

I wanted to do some scheduled JDBC job within a bean.

And I got.

Internal Exception: java.sql.SQLException: Error in allocating a connection.
Cause: java.lang.IllegalStateException: Local transaction already has 1 non-XA Resource:
cannot add more resources.
Error Code: 0
@LocalBean @Stateless
class MyBean {

    public void doJPA() {
    }

    @Schedule
    public void doJDBC() {
    }

    @PersistentContext
    EntityManager entityManager;

    @Resource
    DataSource dataSource;
}

Did I do anything wrong?

Do I just have to split the bean?

Thank you.

Upvotes: 0

Views: 861

Answers (1)

James
James

Reputation: 18389

If you use the same DataSource as JPA is using, the server should give you the same connection, so you should not get this error. Are you using a different data source?

You could also get the JDBC Connection from your EntityManager using unwrap()

See, http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager

Upvotes: 1

Related Questions