Toplink and CMT message driven bean - netbeans

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.

Related

VertX SqlClientApi/SqlConnection Question

The documentation says
The JDBC Client API created in Vert.x 3 is now deprecated and instead the new SQL Client API should be used. It will remain supported for the life time of Vert.x 4 to allow applications to be migrated to the new SQL Client API.
It seems that this class works in an Autocommit-Mode. If I have several database calls within one Service, how should this work with transaction-consistency? Is it planned, that the "commit", "rollback" will also be available as it is in SQLConnection?
Thx
You can take a look at Javadocs of the new client transaction APIs in https://vertx.io/docs/apidocs/io/vertx/sqlclient/Pool.html#withTransaction-java.util.function.Function-, the JDBCClient will execute the block starting with disabling auto-commit mode and ending with a commit or rollback execution.

NServiceBus disposing Autofac Container

Here goes - bear with me:
Two Autofac 4.2.1 Containers:
One in an Asp.NET 4.6.1 WebApi project
One in an NServiceBus 6 host
Both possess an IJobService reference to the JobService (which saves jobs to DynamoDB).
Run the project in Visual Studio...
If I make a WebApi request into the first JobService it succeeds and inserts a record to DynamoDB and drops a command on the bus for NServiceBus to pickup.
During the processing of the Saga, NServiceBus makes a call to JobService again (presumably on the second container) to save progress. This second call fails to insert to DynamoDB with the lifetime disposed. If I try to create anything from IComponentContext I get:
Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
The NServiceBus host is running AsA_Server and I register the container in the Customize method of IConfigureThisEndPoint.
Any pointers on how to see where the lifetime is getting dumped or if it's mysteriously picking the wrong IJobService somehow?
Just to close this one out - we ended up redesigning the solution and moving any web service calls out to their own handlers. That was based off the advice found here http://docs.particular.net/nservicebus/sagas That change resolved the issue one way or another.
Specifically, this guidance:
Other than interacting with its own internal state, a saga should not access a database, call out to web services, or access other resources - neither directly nor indirectly by having such dependencies injected into it.

WebSphere 7.0 remote client rollback global UserTransaction

I'm observing weird behavior for WebSphere 7.0.0.21:
Architecture:
Simple EJB bean with annotation #Local, #Remote Interfaces and transactional method marked as #Required
Standalone command line client that looks up remote "jta/usertransaction" and transactional EJB method. Client code starts user transaction, executes method and then tries to rollback it.
Expected behavior: (I see it on Jboss) rollback of DB transaction
Observed behavior: (On WAS 7.0.0.21) commit of DB transaction
I see that client transaction is changing from STATUS_NO_TRANSACTION(6) to STATUS_ACTIVE(0) and then again to STATUS_NO_TRANSACTION(6) after rollback.
I tried to Google it but didn't find any results
Any ideas on this scenario ? I'm pretty much ready to file the issue to IBM.
thanks,
UPDATE:
Finally after long wait and interactions with IBM support I got it resolved:
No problems with IBM JRE
For Sun/Oracle JRE it requires extra configuration for ORB e.g.
jndiProperties.put("java.naming.corba.orb", com.ibm.CORBA.iiop.ORB.init((String[])null, orbProperties));
and orb.properties from WAS or AppClient JRE is required to be provided as "orbProperties"

How to use Apache-Commons DBCP with EclipseLink JPA and Tomcat 7.x

I've been working on a web application, deployed on Tomcat 7, which use EclipseLink JPA to handle the persistence layer.
Everything works fine in a test environment but we're having serious issues in the production environment due to a firewall cutting killing inactive connections. Basically if a connection is inactive for a while a firewall the sits between the Tomcat server and the DB server kill it, with the result of leaving "stale" connections in the pool.
The next time that connection is used the code never returns, until it gets a "Connection timed out" SQLException (full ex.getMessage() below).
EL Fine]: 2012-07-13
18:24:39.479--ServerSession(309463268)--Connection(69352859)--Thread(Thread[http-bio-8080-exec-5,5,main])--
MY QUERY REPLACED TO POST IT TO SO [EL Config]: 2012-07-13
18:40:10.229--ServerSession(309463268)--Connection(69352859)--Thread(Thread[http-bio-8080-exec-5,5,main])--disconnect
[EL Info]: 2012-07-13
18:40:10.23--UnitOfWork(1062365884)--Thread(Thread[http-bio-8080-exec-5,5,main])--Communication
failure detected when attempting to perform read query outside of a
transaction. Attempting to retry query. Error was: Exception
[EclipseLink-4002] (Eclipse Persistence Services -
2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException Internal
Exception: java.sql.SQLException: Eccezione IO: Connection timed out
I already tried several configuration in the persistence.xml, but since I have no access to the firewall configuration I had no luck with these methods. I also tried to use setCheckConnections()
ConnectionPool cp = ((JpaEntityManager)em).getServerSession().getDefaultConnectionPool();
cp.setCheckConnections();
cp.releaseConnection(cp.acquireConnection());
I managed to solve the issue in a test script using testOnBorrow, testWhileIdle and other features that are avalaible from DBCP Apache Commons. I'd like to know how to override the EclipseLink internal connection pool to use a custom connection pool so that I can provide an already configured pool, based on DBCP rather than just configuring the internal one using persistence.xml.
I know I should provide a SessionCustomizer, I'm uncertain which one is the correct pattern to use. Basically I would like to preserve the performance of DBCP in a JPA-like way.
I'm deploying on Tomcat 7, I know that if I switch to GF I won't have this problem, but for a matter of consistency with other webapp on the same server I'd prefere to stay on Tomcat.
What you want is definitely possible, but you might be hitting the limits of the "do it yourself" approach.
This is one of the more difficult things to explain, but there are effectively two ways to configure your EntityManagerFactory. The "do it yourself" approach and the "container" approach.
When you call Persistence.createEntityManagerFactory it eventually delegates to this method of the PersistenceProvider interface implemented by EclipseLink:
EntityManagerFactory createEntityManagerFactory(String emName, Map map)
The deal here is EclipseLink will then take it upon itself to do all the work, including its own connection creation and handling. This is the "do it yourself" approach. I don't know EclipseLink well enough to know if there is a way to feed it connections using this approach. After two days on Stackoverflow it doesn't seem like anyone else has that info either.
So here is why this "works in GF". When you let the container create the EntityManagerFactory for you by having it injected or looking it up, the container uses a different method on the PersistenceProvider interface implemented by EclipseLink:
EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)
The long and short of it is that this PersistenceUnitInfo is an interface that the container implements and has these two very key methods on it:
public DataSource getJtaDataSource();
public DataSource getNonJtaDataSource();
With this mode EclipseLink will not try to do its own connection handling and will simply call these methods to get the DataSource from the container. This is really what you need.
There are two possible approaches you could take to solving this:
You could attempt to instantiate the EclipseLink PersistenceProvider implementation yourself and call the createContainerEntityManagerFactory method passing in your own implementation of the PersistenceUnitInfo interface and feed the DBCP configured DataSource instances into EclipseLink that way. You would need to parse the persistence.xml file yourself and feed that data in through the PersistenceUnitInfo. As well EclipseLink might also expect a TransactionManager, in which case you'll be stuck unless you hunt down a TransactionManager you can add to Tomcat.
You could use the Java EE 6 certified version of Tomcat, TomEE. DataSources are configured in the tomee.xml, created using DBCP with full support for all the options you need, and passed to the PersistenceProvider using the described createContainerEntityManagerFactory call. You then get the EntityManagerFactory injected via #PersistenceUnit or look it up.
If you do attempt to use TomEE, make sure your persistence.xml is updated to explicitly set transaction-type="RESOURCE_LOCAL" because the default is JTA. Even though it's non-compliant to use JTA with the Persistence.createEntityManagerFactory approach, there aren't any persistence providers that will complain and let you know you're doing something wrong, they treat it as RESOURCE_LOCAL ignoring the schema. So when you go to port your app to an actual certified server, it blows up.
Another note on TomEE is that in the current release, you'll have to put your EclipseLink libs in the <tomcat>/lib/ directory. This is fixed in trunk, just not released yet.
I'm not sure how useful these slides will be without the explanation that goes along with them, but the second part of this presentation is a deep dive into how container-managed EntityManager's work, specifically with regards to connection handling and transactions. You can ignore the transaction part as you aren't using them and already have an in production you're not likely to dramatically change, but it might be interesting for future development.
Best of luck!

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.