Browse Source

Java JSON fix. C# connection fix (#2623)

Separate DB and JSON context.
Update Revenj Java to latest version.

Release connection when thread dies.
zapov 8 years ago
parent
commit
647e818f78

+ 10 - 1
frameworks/CSharp/revenj/Revenj.Bench/Context.cs

@@ -12,13 +12,17 @@ namespace Revenj.Bench
 		public readonly IQueryableRepository<Fortune> FortuneRepository;
 		public readonly Random Random = new Random(0);
 		public readonly World[] Worlds = new World[512];
+		private readonly IDatabaseQueryManager QueryManager;
+		private readonly IDatabaseQuery Query;
 		//private readonly IRepositoryBulkReader BulkReader;
 		//private readonly Lazy<World>[] LazyWorlds = new Lazy<World>[512];
 
 		public Context(IObjectFactory factory, IDatabaseQueryManager manager)
 		{
+			QueryManager = manager;
 			var scope = factory.CreateScope(null);
-			scope.RegisterInterfaces(manager.StartQuery(false));
+			Query = manager.StartQuery(false);
+			scope.RegisterInterfaces(Query);
 			WorldRepository = scope.Resolve<IPersistableRepository<World>>();
 			FortuneRepository = scope.Resolve<IQueryableRepository<Fortune>>();
 			//BulkReader = scope.BulkRead(ChunkedMemoryStream.Static());
@@ -47,5 +51,10 @@ namespace Revenj.Bench
 				worlds[i] = WorldRepository.Find(id);
 			}
 		}
+
+		~Context()
+		{
+			QueryManager.EndQuery(Query, true);
+		}
 	}
 }

+ 3 - 3
frameworks/Java/revenj-jvm/pom.xml

@@ -6,7 +6,7 @@
 	<artifactId>revenj</artifactId>
 	<name>Revenj.JVM</name>
 	<packaging>war</packaging>
-	<version>1.0.2</version>
+	<version>1.0.3</version>
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 	</properties>
@@ -14,7 +14,7 @@
 		<dependency>
 			<groupId>org.revenj</groupId>
 			<artifactId>revenj-core</artifactId>
-			<version>1.0.2</version>
+			<version>1.1.3</version>
 		</dependency>
 		<dependency>
 			<groupId>org.apache.commons</groupId>
@@ -33,7 +33,7 @@
 			<plugin>
 				<groupId>com.dslplatform</groupId>
 				<artifactId>dsl-platform-maven-plugin</artifactId>
-				<version>1.0.1</version>
+				<version>1.1.2</version>
 				<executions>
 					<execution>
 						<phase>generate-sources</phase>

+ 5 - 3
frameworks/Java/revenj-jvm/src/main/java/hello/Context.java

@@ -1,6 +1,5 @@
 package hello;
 
-import com.dslplatform.json.JsonWriter;
 import dsl.Boot;
 import dsl.FrameworkBench.*;
 import dsl.FrameworkBench.repositories.*;
@@ -28,7 +27,6 @@ class Context {
 		}
 	}
 
-	public final JsonWriter json;
 	public final WorldRepository worlds;
 	public final FortuneRepository fortunes;
 	public final Connection connection;
@@ -43,7 +41,6 @@ class Context {
 			this.connection = DriverManager.getConnection(jdbcUrl);
 			connection.setAutoCommit(true);
 			ctx.registerInstance(connection);
-			this.json = new JsonWriter();
 			this.random = ThreadLocalRandom.current();
 			this.worlds = ctx.resolve(WorldRepository.class);
 			this.fortunes = ctx.resolve(FortuneRepository.class);
@@ -83,4 +80,9 @@ class Context {
 		}
 		return buffer;
 	}
+
+	@Override
+	protected void finalize() throws Throwable {
+		connection.close();
+	}
 }

+ 1 - 1
frameworks/Java/revenj-jvm/src/main/java/hello/DbServlet.java

@@ -16,7 +16,7 @@ public class DbServlet extends HttpServlet {
 		res.setContentType("application/json");
 		final Context ctx = Utils.getContext();
 		final Optional<World> world = ctx.worlds.find(ctx.getRandom10k(), ctx.connection);
-		final JsonWriter writer = ctx.json;
+		final JsonWriter writer = Utils.getJson();
 		world.get().serialize(writer, false);
 		writer.toStream(res.getOutputStream());
 	}

+ 1 - 1
frameworks/Java/revenj-jvm/src/main/java/hello/FortunesServlet.java

@@ -16,7 +16,7 @@ public class FortunesServlet extends HttpServlet {
 		final Context ctx = Utils.getContext();
 		final List<Fortune> fortunes = ctx.fortunes.search();
 		fortunes.add(new Fortune(0, "Additional fortune added at request time."));
-		Collections.sort(fortunes, COMPARATOR);
+		fortunes.sort(COMPARATOR);
 		req.setCharacterEncoding("UTF-8");
 		req.setAttribute("fortunes", fortunes);
 		req.getRequestDispatcher("/WEB-INF/jsp/fortunes.jsp").forward(req, res);

+ 1 - 1
frameworks/Java/revenj-jvm/src/main/java/hello/JsonServlet.java

@@ -13,7 +13,7 @@ public class JsonServlet extends HttpServlet {
 	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
 		res.setContentType("application/json");
 		final Message msg = new Message("Hello, World!");
-		final JsonWriter writer = Utils.getContext().json;
+		final JsonWriter writer = Utils.getJson();
 		msg.serialize(writer, false);
 		writer.toStream(res.getOutputStream());
 	}

+ 1 - 1
frameworks/Java/revenj-jvm/src/main/java/hello/QueriesServlet.java

@@ -14,7 +14,7 @@ public class QueriesServlet extends HttpServlet {
 		res.setContentType("application/json");
 		final int count = Utils.parseBoundParam(req);
 		final Context ctx = Utils.getContext();
-		final JsonWriter json = ctx.json;
+		final JsonWriter json = Utils.getJson();
 		final World[] worlds = ctx.loadWorldsSlow(count);
 		json.serialize(worlds, count);
 		json.toStream(res.getOutputStream());

+ 2 - 2
frameworks/Java/revenj-jvm/src/main/java/hello/UpdatesServlet.java

@@ -17,13 +17,13 @@ public class UpdatesServlet extends HttpServlet {
 		res.setContentType("application/json");
 		final int count = Utils.parseBoundParam(req);
 		final Context ctx = Utils.getContext();
-		final JsonWriter json = ctx.json;
+		final JsonWriter json = Utils.getJson();
 		final World[] worlds = ctx.loadWorldsSlow(count);
 		final ArrayList<World> changed = new ArrayList<>(count);
 		for (int i = 0; i < count; i++) {
 			changed.add(worlds[i].setRandomNumber(ctx.getRandom10k()));
 		}
-		Collections.sort(changed, ASC);
+		changed.sort(ASC);
 		ctx.worlds.update(changed);
 		json.serialize(worlds, count);
 		json.toStream(res.getOutputStream());

+ 13 - 12
frameworks/Java/revenj-jvm/src/main/java/hello/Utils.java

@@ -1,24 +1,25 @@
 package hello;
 
+import com.dslplatform.json.JsonWriter;
+
 import javax.servlet.http.HttpServletRequest;
-import java.io.IOException;
 
 abstract class Utils {
 
-	private static final ThreadLocal<Context> threadContext = new ThreadLocal<Context>() {
-		@Override
-		protected Context initialValue() {
-			return new Context();
-		}
-	};
+	private static final ThreadLocal<Context> threadContext = ThreadLocal.withInitial(Context::new);
+	private static final ThreadLocal<JsonWriter> threadJson = ThreadLocal.withInitial(JsonWriter::new);
+
+	static Context getContext() {
+		return threadContext.get();
+	}
 
-	static Context getContext() throws IOException {
-		Context ctx = threadContext.get();
-		ctx.json.reset();
-		return ctx;
+	static JsonWriter getJson() {
+		JsonWriter json = threadJson.get();
+		json.reset();
+		return json;
 	}
 
-	public static int parseBoundParam(HttpServletRequest request) {
+	static int parseBoundParam(HttpServletRequest request) {
 		int count = 1;
 		try {
 			count = Integer.parseInt(request.getParameter("queries"));