OPENJPA-359 Improve Timestamp resolution for use by versioning.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@631028 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Albert Lee 2008-02-25 22:59:29 +00:00
parent c3e2f01204
commit c4af04617e
4 changed files with 171 additions and 1 deletions

View File

@ -49,6 +49,7 @@ import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedBlobFieldStrategy;
import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedByteArrayFieldStrategy;
import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedCharArrayFieldStrategy;
import org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedClobFieldStrategy;
import org.apache.openjpa.jdbc.meta.strats.NanoPrecisionTimestampVersionStrategy;
import org.apache.openjpa.jdbc.meta.strats.NoneClassStrategy;
import org.apache.openjpa.jdbc.meta.strats.NoneDiscriminatorStrategy;
import org.apache.openjpa.jdbc.meta.strats.NoneFieldStrategy;
@ -80,6 +81,7 @@ import org.apache.openjpa.jdbc.sql.JoinSyntaxes;
import org.apache.openjpa.lib.conf.Configurable;
import org.apache.openjpa.lib.conf.Configurations;
import org.apache.openjpa.lib.util.J2DoPrivHelper;
import org.apache.openjpa.lib.util.JavaVersions;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.FieldMetaData;
@ -618,6 +620,8 @@ public class MappingRepository
strat = NumberVersionStrategy.class;
else if (TimestampVersionStrategy.ALIAS.equals(name))
strat = TimestampVersionStrategy.class;
else if (NanoPrecisionTimestampVersionStrategy.ALIAS.equals(name))
strat = NanoPrecisionTimestampVersionStrategy.class;
else if (StateComparisonVersionStrategy.ALIAS.equals(name))
strat = StateComparisonVersionStrategy.class;
@ -1221,7 +1225,9 @@ public class MappingRepository
switch (vfield.getTypeCode()) {
case JavaTypes.DATE:
case JavaTypes.CALENDAR:
return new TimestampVersionStrategy();
return (JavaVersions.VERSION >= 5)
? new NanoPrecisionTimestampVersionStrategy()
: new TimestampVersionStrategy();
case JavaTypes.BYTE:
case JavaTypes.INT:
case JavaTypes.LONG:

View File

@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.jdbc.meta.strats;
import org.apache.openjpa.lib.util.TimestampHelper;
/**
* Uses a timestamp for optimistic versioning with nanosecond
* precision.
*
* @author Albert Lee
*/
public class NanoPrecisionTimestampVersionStrategy
extends TimestampVersionStrategy {
public static final String ALIAS = "nano-timestamp";
public String getAlias() {
return ALIAS;
}
protected Object nextVersion(Object version) {
return TimestampHelper.getNanoPrecisionTimestamp();
}
}

View File

@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.lib.util;
import java.sql.Timestamp;
/**
* Helper class to create java.sql.Timestamp object with nanosecond precision.
* This is only available since JDB 5.
*
* @author Albert Lee
*/
public final class Timestamp5Helper extends TimestampHelper{
// number of seconds passed 1970/1/1 00:00:00 GMT.
private static long sec0;
// fraction of seconds passed 1970/1/1 00:00:00 GMT, offset by
// the base System.nanoTime (nano0), in nanosecond unit.
private static long nano0;
static {
// initialize base time in second and fraction of second (ns).
long curTime = System.currentTimeMillis();
sec0 = curTime / MilliMuliplier;
nano0 = (curTime % MilliMuliplier) * MicroMuliplier - System.nanoTime();
}
/*
* This class implements a nanosecond precision Timestamp.
*/
protected Timestamp getTimestamp() {
long nano_delta = nano0 + System.nanoTime();
long sec1 = sec0 + (nano_delta / NanoMuliplier);
long nano1 = nano_delta % NanoMuliplier;
Timestamp rtnTs = new Timestamp(sec1 * MilliMuliplier);
rtnTs.setNanos((int) nano1);
return rtnTs;
}
}

View File

@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.lib.util;
import java.sql.Timestamp;
/**
* Helper base class attempts to return java.sql.Timestamp object with
* nanosecond precision. This base class is created to allow JDK 1.4 maven build
* and only implements millisecond precision.
*
* @author Albert Lee
*/
public class TimestampHelper {
// number of millisecond, mircoseconds and nanoseconds in one second.
protected static final long MilliMuliplier = 1000L;
protected static final long MicroMuliplier = MilliMuliplier * 1000L;
protected static final long NanoMuliplier = MicroMuliplier * 1000L;
private static TimestampHelper instance = null;
static {
if (JavaVersions.VERSION >= 5) {
try {
Class timestamp5HelperClass = Class
.forName("org.apache.openjpa.lib.util.Timestamp5Helper");
instance = (TimestampHelper) timestamp5HelperClass
.newInstance();
} catch (Throwable e) {
instance = new TimestampHelper();
}
} else {
instance = new TimestampHelper();
}
}
/*
* Return a java.sql.Timestamp object of current time.
*/
public static Timestamp getNanoPrecisionTimestamp() {
return instance.getTimestamp();
}
/*
* This class implements a millisecond precision Timestamp.
*/
protected Timestamp getTimestamp() {
return new Timestamp(System.currentTimeMillis());
}
}