Why must EJB Timer share a database connection in a cluster? - ejb

I have a singleton EJB bean with a timed method that saves statistics to the database once every minute. The bean holds statistics individually on each cluster node, so it is important that it saves it on each node too.
My concern is that since the EJB Timer service is sharing a database, it will only run the save-method on one of the cluster nodes and not all of them. It would mean that not all of the statistics are saved to the database. The docs, http://docs.oracle.com/cd/E18930_01/html/821-2418/beahw.html, does not seem to mention anything.
Anyone know how it works?

If you declare the timer with #Schedule(..., persistent=true), you will get a cluster timer stored in the timer db (which can be migrated), so it will run in only one node.
If you declare the timer with #Schedule(..., persistent=false), you will get a node timer not stored in the timer db (which can't be migrated), so it will be invoked on each node.

Related

What's the basic difference between single record kafka consumer and kafka batch consumer?

I am using spring-kafka 2.2.8 and trying to understand what's the main difference between single record consumer and a batch consumer.
As far as I understand, reading messages/bytes from a topic wouldn't be any different for a single record consumer vs batch consumer. The only difference is how the offset is committed. And hence error handling. Is my understanding correct? Please confirm.
With a record-based listener, the records returned by the poll are handed to the listener one at a time. The container can be configured to commit the offsets one-at-a-time, or after all records are processed (default).
With a batch listener, the records returned by the poll are all handed to the listener in one call.

Does method jdbcSession() in AppServiceHub create a fresh session each time it is called?

Our code uses a custom CordaService that maintains state in a database table. To query/update the table, the service uses a JDBC Connection object obtained by calling AppServiceHub.jdbcSession().
It is not clear from the documentation if this call creates a fresh (not-in-use) JDBC Connection object or if it returns the same Connection to all callers. Since our Corda service exposes methods to flows that execute concurrently, this matters.
The documentation
states that the method
Exposes a JDBC connection (session) object using the currently configured database.
and that the method
Returns a new Connection
The second statement suggests that we should get a fresh Connection on each call, but in reality concurrent calls appear to return the same Connection object.
Can someone clarify what the intended and actual behaviors for this method are?
The second statement is incorrect. The jdbcSession method does not always return a fresh connection object. See https://github.com/corda/corda/issues/4498 (now fixed).
Instead:
The node creates a pool of connections at start-up
If one of these connections breaks, the node adds a new one to the pool
When a flow starts/is restored from a checkpoint, the node gives the flow a connection from this pool
It is possible, but not guaranteed, that by chance a flow will be handed the same connection across multiple suspend/restores
When a flow suspends/ends, the node returns the flow's connection to the pool

Thread execution

I have a web application that checks the user accounts from the database to determine their source. I want to make sure that the thread that goes to check the database runs first without any scheduling algorithm of WebSphere server.
More Clarification:
Even if I define the method at first it takes time to gather all information so I want to make sure that the thread completes getting all the information from the database and proceed to other threads in the server.
Have you tried using javax.servlet.ServletContexetListener.contextInitialized ?
Note that the JavaDoc states "All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized."

Should I bind JDBC connection to servlet thread?

I'm using jndi to access to dbcp datasource.
In scope of one servlet request each time call static_dataSource.getConnection() returns new connection.
In hibernate there is a function getCurrentSession(). As far as I understand this function return connection that binded to current thread. Should I bind my jdbc connection to thread also?
My goal is call complex business logic in scope of one transaction that contains many DAO calls, per one http request. Like in Spring I can annotate whole service class with #Transactional.
I'd recommend that you not bind a JDBC connection to a thread. Use a connection pool and hang onto the connection for the shortest time possible: acquire from pool, perform operation, close the connection, return to pool.
Your persistence code ought to be completely separate from the web tier. You should be able to test and use it independent of the web tier. Anything that you do to tie the two together will diminish the effectiveness of the persistence tier.

Communication between EJB3 Instances (Java EE inter-bean communication) possible?

I'm designing a part of a Java EE 6 application, consisting of EJB3 beans. Part of the requirements are multiple parallel (say a few hundred) long running (over days) database hunts. Individual hunts have different search parameters (start time, end time, query filter). Parameters may get changed over time.
Currently I'm thinking of the following:
SearchController (Stateless Session Bean) formulates a set of search parameters, sends it off to a SearchListener via JMS
SearchListener (Message Driven Bean) receives search parameters, instantiates a SearchWorker with the parameters
SearchWorker (SLSB) hunts repeatedly through the database; when it finds something, the result is sent off via JMS, and the search continues; when the given 'end-time' has reached, it ends
What I'm wondering now:
Is there a problem, with EJB3 instances running for days? (Other than that I need to be able to deal with container restarts...)
How do I know how many and which EJB instances of SearchWorker are currently running?
Is it possible to communicate with them individually (similar to sending a System V signal to a unix process), e.g. to send new parameters, to end an instance, etc..
If you're holding a huge ResultSet open for an extended period of time, you're likely to encounter either transaction timeouts or database locking issues.
There is no builtin mechanism for determining which bean instances are running in a method, so you would need to add your own mechanism. Your product might have some kind of performance monitoring that lets you know how many of each type of bean is currently running a method.
As for cross-thread communication, you would need to implement your own synchronization and periodically check in the bean method. You'll be outside the scope of standard EJB since each parallel call to a business method will allocate a new SLSB from the pool.

Resources