Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tool-up-backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
elis
tool-up-backend
Commits
eee413da
Commit
eee413da
authored
8 years ago
by
t.belmega@gmx.de
Browse files
Options
Downloads
Patches
Plain Diff
Use EHCache for caching BusinessObjects
parent
ffdaa65a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
pom.xml
+9
-0
9 additions, 0 deletions
pom.xml
src/main/java/de/unipotsdam/cs/toolup/database/DatabaseController.java
+42
-5
42 additions, 5 deletions
.../de/unipotsdam/cs/toolup/database/DatabaseController.java
with
51 additions
and
5 deletions
pom.xml
+
9
−
0
View file @
eee413da
...
...
@@ -54,6 +54,12 @@
<artifactId>
commons-lang3
</artifactId>
<version>
3.4
</version>
</dependency>
<dependency>
<groupId>
net.sf.ehcache
</groupId>
<artifactId>
ehcache
</artifactId>
<version>
2.10.2
</version>
</dependency>
</dependencies>
<build>
<finalName>
toolup
</finalName>
...
...
@@ -84,4 +90,7 @@
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
</project>
This diff is collapsed.
Click to expand it.
src/main/java/de/unipotsdam/cs/toolup/database/DatabaseController.java
+
42
−
5
View file @
eee413da
...
...
@@ -2,6 +2,9 @@ package de.unipotsdam.cs.toolup.database;
import
de.unipotsdam.cs.toolup.exceptions.InvalidIdException
;
import
de.unipotsdam.cs.toolup.model.*
;
import
net.sf.ehcache.Cache
;
import
net.sf.ehcache.CacheManager
;
import
net.sf.ehcache.Element
;
import
java.io.IOException
;
import
java.sql.PreparedStatement
;
...
...
@@ -19,13 +22,18 @@ public class DatabaseController {
public
static
final
String
TABLE_NAME_APPLICATION
=
"application"
;
public
static
final
String
TABLE_NAME_APPLICATION_FEATURE
=
"application_has_feature"
;
public
static
final
String
TABLE_NAME_APPLICATION_CATEGORY
=
"application_belongs_to_category"
;
public
static
final
String
TOOL_UP_CACHE
=
"toolUpCache"
;
private
static
final
String
COLUMN_NAME_UUID
=
"uuid"
;
private
static
final
String
COLUMN_SUFFIX_UUID
=
"_"
+
COLUMN_NAME_UUID
;
private
static
DatabaseController
instance
;
private
SqlStatementFactory
sqlStatementFactory
;
private
CacheManager
cacheManager
;
private
Cache
cache
;
protected
DatabaseController
()
throws
IOException
,
SQLException
{
sqlStatementFactory
=
new
SqlStatementFactory
();
initCache
();
}
public
static
DatabaseController
getInstance
()
throws
IOException
,
SQLException
{
...
...
@@ -35,6 +43,13 @@ public class DatabaseController {
return
instance
;
}
private
synchronized
void
initCache
()
{
cacheManager
=
CacheManager
.
newInstance
();
if
(!
cacheManager
.
cacheExists
(
TOOL_UP_CACHE
))
{
cacheManager
.
addCacheIfAbsent
(
TOOL_UP_CACHE
);
}
cache
=
cacheManager
.
getCache
(
TOOL_UP_CACHE
);
}
/**
* Loads all BusinessObjects from a single table.
...
...
@@ -51,22 +66,44 @@ public class DatabaseController {
}
/**
*
Load
s a single business object with the given uuid
.
*
The table to load from is evaluated
from the
i
d.
*
Retrieve
s a single business object with the given uuid
,
*
either from cache or
from the d
b
.
* @param uuid the uuid of the object to load.
* @return the loaded BusinessObject
* @return the loaded BusinessObject
, or the NullBusinessObject if it doesn't exist
* @throws SQLException
* @throws InvalidIdException
*/
public
BusinessObject
load
(
String
uuid
)
throws
SQLException
,
InvalidIdException
{
Element
cachedElement
=
cache
.
get
(
uuid
);
if
(
cachedElement
!=
null
)
{
System
.
out
.
println
(
"Use BO from cache: "
+
uuid
);
Object
cachedObject
=
cachedElement
.
getObjectValue
();
return
(
BusinessObject
)
cachedObject
;
}
else
{
return
loadBOFromDatabase
(
uuid
);
}
}
/**
* Loads a single business object with the given uuid from the database.
* The table to load from is evaluated from the id.
*/
private
BusinessObject
loadBOFromDatabase
(
String
uuid
)
throws
InvalidIdException
,
SQLException
{
String
tableName
=
BusinessObject
.
getTableNameFromId
(
uuid
);
PreparedStatement
prepQuery
=
sqlStatementFactory
.
getStatementSelectByUuidFrom
(
tableName
);
prepQuery
.
setString
(
1
,
uuid
);
ResultSet
res
=
prepQuery
.
executeQuery
();
return
BusinessObjectFactory
.
createBusinessObjectFromSingleResult
(
res
);
BusinessObject
businessObjectFromSingleResult
=
BusinessObjectFactory
.
createBusinessObjectFromSingleResult
(
res
);
putBOIntoCache
(
businessObjectFromSingleResult
);
return
businessObjectFromSingleResult
;
}
private
void
putBOIntoCache
(
BusinessObject
businessObjectFromSingleResult
)
{
Element
element
=
new
Element
(
businessObjectFromSingleResult
.
getUuid
(),
businessObjectFromSingleResult
);
cache
.
put
(
element
);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment