将SQLite与我的Java EE应用程序集成的正确方法是什么? [英] What's the right way to integrate SQLite with my Java EE app?

查看:93
本文介绍了将SQLite与我的Java EE应用程序集成的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一个非常简单的SQLite数据库添加到现有的Java EE应用程序中.我非常习惯于使用EJB,ORM,EntityManager等来处理SQL数据库.我从来没有使用某种ORM来处理Java中的关系数据库.

I'm looking to add a pretty simple SQLite database to an existing Java EE application. I'm very used to using EJBs, ORMs, EntityManager, etc. to work with SQL databases. I've never not used some sort of ORM to work with relational DBs in Java.

我被推荐" 以使用用于SQLite的Java包装器,而不是JDBC驱动程序-因此,我有点赤裸裸,没有ORM(是吗?).我想快速启动并运行它.

I've been "recommended" to use a Java wrapper for SQLite, rather than a JDBC driver - so I'm kind of naked and ORM-less (right?). I'd like to get this up and running quickly.

我一直在使用内存缓存,实现为Map,随着时间的流逝,它会被条目线性填充.在某个时候,例如当应用程序运行一整夜时,缓存将使用所有可用的堆空间.因此,将缓存存储在磁盘(作为SQLite数据库)而不是内存(作为Java Map)中.

I've been using an in-memory cache, implemented as a Map, which gets filled with entries linearly over time. At some point, like when the app runs overnight, the cache will use all available heap space. Hence, storing the cache on disk (as a SQLite database) rather than in memory (as a Java Map).

  • 我应该如何管理 SQLiteConnection 之类的资源?通常,我会让容器为所有这些事情担心,但是由于我没有使用JDBC,因此必须做所有 !@#$%ing,无增值 sub>手动操作-是吗?
  • 是否有一种方法可以干净透明实施?我希望能够 just 换出一个实现类-例如将FooMapCacheImpl替换为FooSQLiteCacheImpl.
  • "[Most]方法仅限于用于打开连接的线程".是否有一种简单明了的方法来确保我不尝试访问SQLiteConnection是从打开它的线程之外的其他线程获取的?
  • ...以及该问题的另一面:每次我想从数据库读取/写入数据库时​​,是否可以避免创建新的连接? PITA似乎必须按线程而不是按实例管理连接,这真是 .过去,这就是我一直在考虑与数据库进行通信的方式.
  • How should I manage resources like SQLiteConnection? Normally I would let the container worry about all this, but since I'm not using JDBC, I have to do all this !@#$%ing, non-value-added stuff manually - right?
  • Is there a way to implement this cleanly and transparently? I'd like to be able to just swap out an implementing class - e.g. replace FooMapCacheImpl with FooSQLiteCacheImpl.
  • "[Most] methods are confined to the thread that was used to open the connection". Is there a simple, straightforward way to ensure that I don't try to access a SQLiteConnection from threads other than the one that opened it?
  • ...and the flip side of that question: can I avoid creating a new connection every time I want to read from/write to the database? It seems a bona fide PITA to have to manage connections per-thread rather than, say, per instance, which is how I've been thinking about communicating with databases in the past.

在不使用ORM的情况下使用Java/Java EE中的数据库时,我感到很迷茫.
正确的方法是什么?

I'm rather lost when it comes to working with databases in Java/Java EE, without using an ORM.
What's the right way to do it?

推荐答案

我认为制作一个可以实现Map并使用JDBC将所有内容保存到数据库的前端并不难,但是在这样做之前,请考虑一下关于它的两次.整个系统的性能可能会受到严重影响.

I don't think It is too hard to make a front end that would implements Map and save everything to a database using JDBC, but before doing it, think twice about it. The performance of the whole system might be affected badly.

但是,如果问题的根本原因是缺少堆空间,则应查看 Terracotta的BigMemory .但是,它是一种商业(非免费)产品.

However, if the root cause of your problem is the lack of Heap space, you should take a look at Terracotta's BigMemory. However, it is a commercial (non-free) product.

Terracotta还有一个非常好的缓存框架,并且(ehcache)是开源的.看看食谱,这可能会启发人.

Terracotta has a pretty good cache framework as well (ehcache) which is opensource. Look at the cookbook, it might be inspiring.

如果您想手动完成所有操作,并且不介意使用Spring,请尝试spring-jdbc.与任何JDBC驱动程序集成非常容易.看看 SimpleJdbcTemplate .它为您完成所有样板代码.您可能还应该使用连接池,例如 commons-dbcp

If you want to do everything by hand, and you don't mind using Spring, try spring-jdbc. It is very easy to integrate with any JDBC driver. Take a look at SimpleJdbcTemplate. It does all the boiler plate code for you. You should probably use a connection pool as well, such as commons-dbcp

最容易使用的SQLite JDBC驱动程序是.由于它不依赖于JNI.它可能没有那么快,但是对于快速测试而言,它是完美的.

The easiest SQLite JDBC driver to use is this one. Since it doesn't rely on JNI. It might not be as fast, but for quick testing it is perfect.

如果您未绑定到SQLite,则可以查看其他可用的JDBC解决方案,例如 hsqldb 德比

If you aren't binded to SQLite, you can take a look at other available JDBC solutions such as hsqldb or derby

希望这对您有所帮助.

这篇关于将SQLite与我的Java EE应用程序集成的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆