`

LiferayCounter机制

阅读更多
public long increment(String name, int size)
		throws SystemException {

		if (size < _MINIMUM_INCREMENT_SIZE) {
			size = _MINIMUM_INCREMENT_SIZE;
		}

		CounterRegister register = getCounterRegister(name);//从hashMap中取出

		synchronized (register) {
			long newValue = register.getCurrentValue() + size;

			if (newValue > register.getRangeMax()) {//理解这一句提高性能的策略:)
				Session session = null;

				try {
					Connection connection = getConnection();

					session = new SessionImpl(
						_sessionFactory.openSession(connection));

					Counter counter = (Counter)session.get(
						Counter.class, register.getName());

					newValue = counter.getCurrentId() + 1;

					long rangeMax =
						counter.getCurrentId() + register.getRangeSize();

					counter.setCurrentId(rangeMax);

					session.save(counter);
					session.flush();

					register.setCurrentValue(newValue);
					register.setRangeMax(rangeMax);
				}
				catch (Exception e) {
					throw processException(e);
				}
				finally {
					session.close();
				}
			}
			else {
				register.setCurrentValue(newValue);
			}

			return newValue;
		}
	}

 com.liferay.counter.service.persistence.CounterPersistence

 

 

HeatBeat机制:

http://issues.liferay.com/browse/LPS-557 

LEP-7681 addressed an issue where the counter service caused the connection pool to run out of connections. This fix works well for the most part, but there is a side effect where the database terminates the jdbc connection on deployments that have long periods of inactivity (mysql default is 8 hours).

The most generic solution for this is to create a Job that will sent heartbeats (a simple query) to let the database know that this connection is still alive at a default interval of 60 minutes. This is configurable via portal-ext.properties:

#
# Set the interval in minutes for the ConnectionHearbeatJob.
# This will determine how often the database is polled for long running
# connections and will prevent the database from disconnecting the socket
# prematurely.
#
counter.connection.heartbeat.job.interval=60
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics