English 中文(简体)
分类误差
原标题:Catch truncation errors

我有少量应用,有一个嵌入式数据库。 有时 在试图插入超出相应数据库一栏最高规模的斜体时,会出现拖网错误。

我希望在插入/更新之前发现这一点,并向用户发出正确的信息。

Now I presume that there are two possibilities to achieve this.

  1. Get the maximum length of the column of interest through the DatabaseMetaData object. You could reduce the performance lack by using Singletons or similar constructions.

  2. Keep the maximum lengths in the Java code (eg: in ResourceBundle or Properties file) and check against these values. Downside is off course that Java code and database must be in sync. This is error prone.

What would be the best approach?

问题回答

唯一需要维护的答案是,数据库连接时间的利息一栏的最大长度。

If you use Integer.valueOf(...) you can store this in an object, which the lower values (according to the current JVM specs) backs to a singleton pool anyway. This will unload a lot of memory performance issues, as all the columns will eventually refer to the few unique values you likely have in your database.

此外,在数据库MetaData周围挖掘,我想看任何旗帜,显示一栏在数据加后会缩小。 它可以提供开关,了解是否需要你的代码。

By putting the values in a property file, you ease the detection of the issue, but at the cost of possibly getting them out of sync. Such techniques are effectively quick implementations with little up-front cost, but they create latent issues. Whether the issue ever gets raised will be unknown, but given enough time, even the remote possibilities are encountered.

Combination of both the approaches. During application build time, you use DatabaseMetaData to dynamically create a resource bundle.

一种解决办法是使用CLOB。 我不知道你在这方面有什么其他要求。

Also, Use the smallest max character value you have as a constant in the java code. This handles it having to be in sync or db dependent and it s more or less arbitrary anyway. Users don t care what the max size is, they just need to know what the max size is or be kept from making an error automatically.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签