Town building (think before you code.jpg)

preface

Yesterday I saw a question, “What would you like to eat most after the epidemic is over?”

On second thought, hot pot? Barbecue?

Checking the scale, I’m afraid I have to sign up for a gym.

You think the time complexity of your weight is O(2^N), but it’s O(1), whoosh.

The body of the

Get back to business

When it comes to remote debugging, most ides come with it, but it’s rarely used, probably because…

Just a joke😄, don’t take it seriously

After switching to IDEA, I really did not use remote Debug until yesterday, when I found a very basic error…

Come from the pit

The pit comes from my open source gadget, V-Mock.

The author’s intention is to create a simple, lightweight, one-button interface simulation system, which is used to facilitate the front end and back end students waiting for others interface.

For this purpose, I used the embedded database SQLite, in conjunction with Springboot, to construct a small jar package with no configuration and a one-line boot. The directory structure is as follows, and the database is thrown directly into the Resource:

After open source, some students raised a Bug, and the author also operated normally. After correcting the Bug, I re-typed the version and issued it.

If you upgrade the version, you find that the data is missing, so the author gives a temporary solution: for embedded database, you can overwrite the DB files in the old JAR into the new JAR

(DB file location in jar package)

Springboot+Sqlite is the product of occasional attempts at tool smallness.

In fact, if you think about it for a moment, db files, unlike other resources, are frequently overwritten, not the original jar files.

Until I received an issue telling me that copying DB files to the new JAR did not take effect.

The author also quickly realized how possible to use jar DB file, the real file is no accident in java.io. Tmpdir.

Java.io. Tmpdir: $tmpdir for macOS and %temp% for win.

I also switch to the corresponding directory and finally see the DB file that the JAR runtime actually uses:

But this is a strange way of naming it, and it doesn’t have anything to do with the original V-mock. sqlite.

All the way to follow the sqlite JDBC driver source code, find the org. Sqlite. SQLiteConnection extractResource method, saw the name code:

Sqlite-jdbc-tmp concatenates the hashCode URL class of the DB file in the original JAR as the filename.

The reason why I didn’t notice it when I was developing it is to look at this method for the first if judgment.

I prefer to start projects directly with Springboot or Application mode in the IDE, not packaged

So when Protocol is a file instead of a JAR, the target/classes/db/ V-mock. sqlite file is used directly, without generating temporary files.

At the time of development, the DB visualization tool was also connected to target/classes/ DB/V-mock. sqlite, so there was no problem.

In fact, this is very normal operation, many places of the source code to determine whether the normal Web environment or jar to run, if this aspect of debugging, to think about your startup.

So if you want to hit the breakpoint after the first if and see the effect, one option is to use remote Debug.

Remote Debug of IDEA

IDEA remote Debug module is really designed very considerate, stupid operation, commands are generated, I do not know the current version of Eclipse is not so considerate.

Search for the Remote template from Configuration and click Create Configuration in the upper right corner to create a remote Debug startup.

The Debugger mode option is Attach to the remote JVM and Listen to the remote JVM, as the name suggests.

IP and port no need to say more, I directly use the local JAR package, so fill localhost, the right JDK version if using other versions, need to adjust.

The text box in the middle is the generated JVM parameters, very user-friendly, Java-jar-agentlib: JDWP =transport=dt_socket,server=y,suspend=n,address=5005 v-mock.jar

Don’t worry about what the command means. If you want to know, I’ll explain:

  • – Agentlib: The most important parameter of JDWP, starts the JDWP agent. JDWP stands for Java Debug Wire Protocol.

  • Transport = dT_socket Transmits data over sockets. Dt is an abbreviation for data Transfer.

  • Server =y =y =y =y =y =y =y =y =y =y =y

  • Suspend =n is set to N, which means that the program is running normally and should Attach whenever it needs to. If set to Y, the program will wait for the debugger to Attach before resuming execution, such as in a debug scenario that starts the source code.

  • Address =5005 Set the debugging port to 5005, but other ports can also be used.

Start the JAR package, debug as you just created, and the desired breakpoint location has been successfully reached.

At the end

After finding the reason, the author also along sqliteJDBC source code named way, developed a way of data migration

That is, automatically or manually find the database file of the previous version, copy it, and name it with the hashCode of the new version.

Springboot+ embedded database, is a very “not serious” combination, but the development of small tools or pretty good, if there is the same data migration problem, may wish to refer to the author’s solution 😄


Take a small chestnut 🌰 other series:

  • For example, 🌰SpringCloud integrates Elastic Stack

  • For example, 🌰 introduces you to the Java diagnostic tool “Arthas”