what is the lifecycle of a contract? #fabric-chaincode


As far as I know, a contract is an instance class with the contract annotation.

When does the Fabric runtime instantiate the contract class?  When does the instance get disposed?

From my experience, if a chaincode has static classes, the values of fields in the static classes persists across multiple CLI commands, like peer chaincode query, peer chaincode invoke, etc.

If I save the instance of a contract to a static field, is it ok?

Like the following code, I'm doing something funny that CreateAsset caches the contract instance to AssetTransfer.instance, and calls that instance instead of this. What consequences will I suffer?
public final class AssetTransfer implements ContractInterface {
private static AssetTransfer instance;
@Transaction(intent = Transaction.TYPE.SUBMIT)
public Asset CreateAsset(final Context ctx, final String assetID, final String color, final int size,
final String owner, final int appraisedValue) {
if (instance == null) {
instance = this;
return instance.CreateAsset(ctx,assetID, color, size, owner, appraisedValue);
ChaincodeStub stub = ctx.getStub();
if (AssetExists(ctx, assetID)) {
String errorMessage = String.format("Asset %s already exists", assetID);
throw new ChaincodeException(errorMessage, AssetTransferErrors.ASSET_ALREADY_EXISTS.toString());

I know this is a huge topic. Is there an online documentation available on this? Can you point out some links?

Join fabric@lists.hyperledger.org to automatically receive all group messages.