java.io.NotSerializableException Error in ObjectInputStream - java

I am trying to save obj in files but java.io.NotSerializableException error doesnt allow me to do it.
This is my code:
import Estrategia.Gestor_nivel;
import Resources.Exceptions.DuplicateLevelsId;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import pp_epoca_normal.Gerir_jogo;
public class Teste {
public void guardar_ficheiro(Object obt) {
Gerir_jogo teste = (Gerir_jogo) obt;
System.out.println("sad---- " + teste);
Gestor_nivel sad;
try {
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("teste.dat"));
ObjectOutputStream objOut1 = new ObjectOutputStream(new FileOutputStream("teste1.dat"));
objOut.writeObject(teste);
objOut1.writeObject(teste.getObjetos());
sad = (Gestor_nivel) teste.getLevel(0);
objOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void ler_ficheiro() throws FileNotFoundException, IOException, ClassNotFoundException, DuplicateLevelsId {
Gerir_jogo asd;
ObjectInputStream mySecondStream = new ObjectInputStream(new FileInputStream("teste.dat"));
ObjectInputStream mySecondStream1 = new ObjectInputStream(new FileInputStream("teste1.dat"));
asd = (Gerir_jogo) mySecondStream.readObject();
asd.setObjetos((Object[]) mySecondStream1.readObject());
System.out.println("leu--");
mySecondStream.close();
mySecondStream1.close();
}
}
Class gerir jogo
import Container.Contentor;
import Estrategia.Gestor_nivel;
import Resources.Exceptions.DuplicateLevelsId;
import Resources.GameContainerContract;
import Resources.GameLevelContract;
public class Gerir_jogo extends Contentor implements GameContainerContract {
private String nome_jogo;
private boolean mode_jogo_depuracao;
public Gerir_jogo(String nome_jogo, boolean mode_jogo) {
this.nome_jogo = nome_jogo;
this.mode_jogo_depuracao = mode_jogo;
}
#Override
public boolean addNewLevel(GameLevelContract glc) throws DuplicateLevelsId {
Gestor_nivel a;
boolean asd = false;
for (Object objetos : this.getObjetos()) {
a = (Gestor_nivel) objetos;
if(objetos != null)
if (a.getId() == glc.getId()) {
asd = true;
}
}
if (asd == false)
return super.addObject(glc);
return false;
}
#Override
public boolean removeLevel(GameLevelContract glc) {
return super.Encontrar_objeto(super.removeObject(super.findObject(glc)));
}
#Override
public GameLevelContract getLevel(int i) {
return (GameLevelContract) super.getObject(i);
}
#Override
public int getSize() {
return super.getCont();
}
#Override
public boolean getDebugMode() {
return this.mode_jogo_depuracao;
}
#Override
public void setDebugMode(boolean bln) {
this.mode_jogo_depuracao = bln;
}
#Override
public void setName(String string) {
this.nome_jogo = string;
}
#Override
public String getName() {
return this.nome_jogo;
}
Can someone help me please?
I really need to put this saving and reading files and what is inside them
Error:
java.io.NotSerializableException: pp_epoca_normal.Gerir_jogo
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Gravar_ler_ficheiro.Teste.guardar_ficheiro(Teste.java:34)
at pp_epoca_normal.PP_epoca_normal.main(PP_epoca_normal.java:77)
I really dont know what to do

Your class Gerir_jogo must implements the interface Serializable. Please, take a look at this tutorial.
Edit 1
Your class does not implements it, so, change if for:
import java.io.Serializable;
public class Gerir_jogo extends Contentor implements GameContainerContract, Serializable {

Implement Serializable on Gerir_jogo class.
Change class declaration as:
import java.io.Serializable;
public class Gerir_jogo extends Contentor implements GameContainerContract, Serializable {

To serialize java object means to stream the byte information as non-static states of the object to external file. Here the class must be implemented by the interface "Serilizable".
Here I have replied with a code, which copies the States as id, name & sal of the class "Employee" to an external file present at the destination "D:\Object\States.txt". Here its not readable.
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Employee implements Serializable {
int id;
String name;
double sal;
public Employee(int id, String name, double sal) {
super();
this.id = id;
this.name = name;
this.sal = sal;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", sal=" + sal + "]";
}
public static void main(String[] args) throws Exception {
Employee e1 = new Employee(101, "Mr. S.V. Roshan", 45000.0D);
FileOutputStream fout = new FileOutputStream("D:/Object/State.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(e1);
out.flush();
out.close();
fout.flush();
fout.close();
System.out.println("Object serialized successfully!");
}
}
I think, It may help you..!

Related

Unable to set Pojo Object variable with an external value during JaxB MoXY Unmarshalling

I am trying the above approach that you have mentioned for NodeAdaptar, but I am getting null for my element.
Scenario:
I have to set the value of variable defined in my Java Model(POJO) Class with a value, coming externally(not part of the input XML). This value will be used in some calculation post unmarshal.
input XML: its a huge one. I am able to unmarshall it correctly.
POJO Class:
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="root")
#XmlAccessorType(XmlAccessType.FIELD)
public class MainModelClass {
#XmlPath("abc/def/Result/text()")
#XmlElement(name="Result")
private String result;
#XmlPath("/abc/def/Score/text()")
#XmlElement(name="Score")
private String score;
///This is where I want to populate the external value ///
#XmlElement
#XmlJavaTypeAdapter(PCNDateAdaptar.class)
private PCNDate pcnDateObject;
public PCNDate getPcnDateObject() {
return pcnDateObject;
}
public void setPcnDateObject(PCNDate pcnDateObject) {
this.pcnDateObject = pcnDateObject;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
// Block for overridden toString() //
}
Adaptar Class:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class PCNDateAdaptar extends XmlAdapter<Integer, PCNDate> {
private PCNDate pcnDateObject;
public void setPcnDateObject(PCNDate pcnDateob) {
this.pcnDateObject = pcnDateob;
}
#Override
public PCNDate unmarshal(Integer v) throws Exception {
if(v == null)
return this.pcnDateObject;
else
return this.pcnDateObject;
}
#Override
public Integer marshal(PCNDate v) throws Exception {
if(v == null)
return null;
else
return null;
}
}
PCNDate.class:
public class PCNDate {
private Integer processControlDate;
public Integer getProcessControlDate() {
return processControlDate;
}
public void setProcessControlDate(Integer pcn) {
this.processControlDate = pcn;
}
}
Unmarshaller Method:
public static <T> T getXMLSnippetObject(String xmlString, Class<T> modelClass, XmlAdapter<?, ?> xmlAdapterObject) throws XMLStreamException, JAXBException {
JAXBContext context = JAXBContext.newInstance(modelClass);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setAdapter(xmlAdapterObject);
InputStream xmlInputStream = IOUtils.toInputStream(xmlString);
XMLInputFactory2 xmlInputFactory = (XMLInputFactory2)XMLInputFactory.newInstance();
xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
xmlInputFactory.setProperty("javax.xml.stream.isNamespaceAware", true);
xmlInputFactory.setProperty("javax.xml.stream.isReplacingEntityReferences", true);
xmlInputFactory.setProperty(XMLInputFactory2.P_AUTO_CLOSE_INPUT, true);
//xmlInputFactory.setProperty(XMLInputFactory2.P_DTD_OVERRIDE, false);
xmlInputFactory.setProperty(XMLInputFactory2.P_REPORT_PROLOG_WHITESPACE, false);
xmlInputFactory.setProperty(XMLInputFactory2.P_PRESERVE_LOCATION,false);
xmlInputFactory.setProperty(XMLInputFactory2.P_INTERN_NS_URIS, true);
XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream);
T objectInstance = (T) JAXBIntrospector.getValue(unmarshaller.unmarshal(xmlStreamReader, modelClass));
return objectInstance;
}
Main Calling Class:
public class XMLparsing {
public static void main(String[] args) throws XMLStreamException, JAXBException, IOException {
String xmlString = // Xml String //
MainModelClass modelObj = null;
Integer pcnDate = 20171010;
// PCNDate & PCNDateAdapter are both no-arg-constructor classes for JAXB purpose
PCNDate pcnObj = new PCNDate();
pcnDateObj.setProcessControlDate(pcnDate);
PCNDateAdaptar pcndateAdaptar = new PCNDateAdaptar();
pcndateAdaptar.setPcnDateObject(pcnObj);
modelObj = XmlUtilsStAX.getXMLSnippetObject(xmlString, MainModelClass.class, pcndateAdaptar);
}
Result:
The whole Xml String is getting correctly parsed. Only MainModelClass's pcnDateObject is null, result & score have values. I want pcnDateObject to have 20171010.
I don't know, what I am missing, please help.

Serialization - not working

I create class with methods like a How to serialize an object into a string
and it every say error "java.lang.ClassCastException: java.lang.String cannot be cast to Myclass"
My codes:
1)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import javax.xml.bind.DatatypeConverter;
public class Serialization {
public static Object fromString(String s) throws IOException,
ClassNotFoundException {
byte[] data = DatatypeConverter.parseBase64Binary(s);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
data));
Object o = ois.readObject();
ois.close();
return o;
}
public static String toString(Serializable o) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return DatatypeConverter.printBase64Binary(baos.toByteArray());
}
}
2) - calling
MyClass hl = (MyClass) Serialization.fromString(items
.getString("data"));
hl.load(); // this is my method from class
3) MyClass - Hologram
public class Hologram implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Location loc;
private String name;
private String displayname;
public ArmorStand stand;
public Hologram(String name, String displayname, Location loc) {
this.loc = loc;
this.name = name;
this.displayname = displayname;
ArmorStand as = (ArmorStand) loc.getWorld().spawnEntity(loc,
EntityType.ARMOR_STAND);
as.setGravity(false);
as.setCanPickupItems(false);
as.setCustomName(displayname);
as.setCustomNameVisible(true);
as.setVisible(false);
this.stand = as;
HologramManager.holograms.put(name, this);
}
public void move(Location loc) {
this.loc = loc;
stand.teleport(loc);
}
public Location getLocation() {
return this.loc;
}
public void remove() {
stand.remove();
HologramManager.holograms.remove(name);
}
public void removeHologram() {
HologramManager.remove(name);
}
public void changeName(String name) {
HologramManager.holograms.remove(this.name);
this.name = name;
HologramManager.holograms.put(name, this);
}
public void changeDisplayName(String displayName) {
this.displayname = displayName;
stand.setCustomName(displayname);
stand.setCustomNameVisible(true);
}
public void load() {
//todo
}
}
Based on the linked answer, the problem most likely lies in the code you aren't showing us. When you serialize your MyClass object, you are probably doing something like this:
MyClass hl;
String base64String = Serialization.toString(hl.toString());
However you should be calling it like this:
MyClass hl;
String base64String = Serialization.toString(hl);
If you pass a String to the serialization function, you'll get a String back when you call Serialization.fromString(). You want to get an object back that you can cast to a MyClass instance, so pass one of those into Serialization.toString().
The fromString() method in Serilization returns an Object, which you wouldnt be able to cast to the class MyClass. The below line is causing the classCastException
MyClass hl = (MyClass) Serialization.fromString(items
.getString("data"));

A class that extends HashMap when adding new variable can't be write to json

ResultContentExt extends HashMap and has it's own variable testInt.
In main method, I write the ResultContentExt been to json.but can't write the variable
testInt to json.
I also rewrite writeObject method in ResultContentExt, which seems not be called...
package test.open.serial;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class ResultContentExt extends HashMap<String, Object> implements
Serializable {
private static final long serialVersionUID = 628377769976336650L;
public ResultContentExt() {
}
private int testInt;
public int getTestInt() {
return testInt;
}
public void setTestInt(int testInt) {
this.testInt = testInt;
}
public List<String> getRef_hot_status() {
return (List<String>) this.get("ref_hot_status");
}
#Override
public String toString() {
return "ResultContentExt [ref_hot_status=" + this.getRef_hot_status()
+ "]" + "ref_new_status=" + this.getRef_new_status()
+ this.getTestInt();
}
public void setRef_hot_status(List<String> ref_hot_status) {
if (ref_hot_status != null) {
this.put("ref_hot_status", ref_hot_status);
} else {
this.put("ref_hot_status", new LinkedList<Map<String, Integer>>());
}
}
public void setRef_new_status(List ref_new_status) {
if (ref_new_status != null) {
this.put("ref_new_status", ref_new_status);
} else {
this.put("ref_new_status", new LinkedList<Map<String, Integer>>());
}
}
public List<String> getRef_new_status() {
return (List<String>) this.get("ref_new_status");
}
public void writeObject(ObjectOutputStream s) throws IOException {
System.out.println("ResultContentExt begin...");
s.defaultWriteObject();
System.out.println("ResultContentExt end");
}
}
the result is {"ref_new_status":["bbbb","cccc"],"ref_hot_status":["aaa","侠盗飞"]}
why the testInt can't be write to json.
the main class
public class SearilazizeTest implements Serializable{
private static final long serialVersionUID = 5767426158258564918L;
private static Logger logger = Logger.getLogger(SearilazizeTest.class);
public static void main(String[] args){
try {
jsonObjectTrans();
} catch (Exception e) {
logger.error("", e);
}
}
public static void jsonObjectTrans(){
ResultContentExt rce = new ResultContentExt();
List<String> refHostStatus = new ArrayList<String>();
refHostStatus.add("aaa");
refHostStatus.add("侠盗飞");
rce.setRef_hot_status(refHostStatus);
List<String> refNewStatus = new ArrayList<String>();
refNewStatus.add("bbbb");
refNewStatus.add("cccc");
rce.setRef_new_status(refNewStatus);
rce.setTestInt(22);
System.out.println(rce.getTestInt());
JSONObject jsonArray = JSONObject.fromObject(rce);
System.out.println(jsonArray);
ResultContentExt packageVersionMaps = new ResultContentExt();
try {
JSONObject jsonObject = JSONObject.fromObject(jsonArray);
packageVersionMaps = (ResultContentExt) JSONObject.toBean(jsonObject,ResultContentExt.class);
} catch (Exception e) {
logger.error("", e);
}
System.out.println(packageVersionMaps);
}}

How to get methods inside a class

So I'm working on a plugin loader for a game and what it does is load plugins (class objects) from a .jar inside a folder, then add them to an ArrayList (so users can make their own mods). But my java experience isn't the best :/ So far it gets the classes in the jar but I need to check what methods are in the class (such as the constructor for labeling). Or if someone knows a better way to do this. Any help would be appreciated.
Plugin.java:
package me.rigamortis.faurax.plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class Plugin {
public Minecraft mc = Minecraft.getMinecraft();
final File folder = new File(mc.mcDataDir + File.separator + "Faurax/Plugins");
public String jar;
public static String className;
private String name;
private String desc;
private int color;
private int key;
public Plugin() {
if(!folder.exists())
folder.mkdirs();
loadPlugins(folder);
if(folder != null)
getClassNames(folder);
}
public void setName(String newName) {
name = newName;
}
public void setDesc(String newDesc) {
desc = newDesc;
}
public void setColor(int newColor) {
color = newColor;
}
public void setKey(int newKey) {
key = newKey;
}
public String getName() {
return this.name;
}
public String getDesc() {
return this.desc;
}
public int getKey() {
return this.key;
}
public int getColor() {
return this.color;
}
/**
* A hook for plugins
*/
public void onTick() {}
public void loadPlugins(final File folder) {
try{
for (final File fileEntry : folder.listFiles()) {
if ( !fileEntry.isDirectory()) {
if(fileEntry.getName().endsWith(".jar")){
jar = fileEntry.getName();
Core.logNormal("Loading " + fileEntry.getName());
}
}
}
}
catch(Exception e) {
Core.logError("Error? jar isin't a plugin.");
e.printStackTrace();
}
}
public void getClassNames(final File dir) {
try{
ZipInputStream zip = new ZipInputStream(new FileInputStream(dir + File.seperator + jar));
for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry())
if(entry.getName().endsWith(".class") && !entry.isDirectory()) {
Core.logNormal("Found "+entry);
className = entry.toString();
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
PluginLoader.java:
package me.rigamortis.faurax.plugin;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import me.rigamortis.faurax.core.Core;
public class PluginLoader {
public static List<Plugin> plugins = new ArrayList<Plugin>();
public PluginLoader() {
loadPlugins();
}
public void loadPlugins() {}
}
I answered a similar question to this not too long ago. Here's the code to get all methods from a given class, I think it's fairly self-explanatory:
void foo(Object o)
{
Class<?> c = o.getClass();
for (Method m : c.getDeclaredMethods())
{
//do whatever
}
}
This takes advantage of the Java's Reflection API. You can read more about it here.

Spring transaction issue

I'm having problems with Spring transactions. I really need help as I can't figure out why personsDao2 is not rolled back as should (see assert below commented with "FAILS!"). Any input?
My Eclipse project is available for download at http://www52.zippyshare.com/v/4142091/file.html. All dependencies are there so it's easy to get going.
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
public class MyInnerClass {
private PersonsDao personsDao;
public MyInnerClass() {
}
public PersonsDao getPersonsDao() {
return personsDao;
}
public void setPersonsDao(PersonsDao personsDao) {
this.personsDao = personsDao;
}
#Transactional(propagation = Propagation.REQUIRES_NEW, noRollbackFor = Exception.class)
public void method() {
personsDao.createPersons(Lists.newArrayList(new Person("Eva")));
}
}
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
public class MyOuterClass {
private MyInnerClass myInnerClass;
private PersonsDao personsDao;
public MyInnerClass getMyInnerClass() {
return myInnerClass;
}
public void setMyInnerClass(MyInnerClass myInnerClass) {
this.myInnerClass = myInnerClass;
}
public void setMyInnerClass() {
}
public PersonsDao getPersonsDao() {
return personsDao;
}
public void setPersonsDao(PersonsDao personsDao) {
this.personsDao = personsDao;
}
public MyOuterClass() {
}
#Transactional(propagation = Propagation.REQUIRED, rollbackFor=Exception.class)
public void method() {
try {
personsDao.createPersons(Lists.newArrayList(new Person("Adam")));
throw new RuntimeException("Forced rollback");
} finally {
myInnerClass.method();
}
}
}
public class Person {
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return "Customer [name=" + name + "]";
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
private String name;
}
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
public class PersonsDao {
public PersonsDao(DataSource dataSource, String tableName) {
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
this.tableName = tableName;
}
public List<Person> getPersons() {
Map<String, Object> namedParameters = new HashMap<String, Object>();
String getCustomers = "SELECT name FROM " + tableName + " ORDER BY name ASC";
return namedParameterJdbcTemplate.query(getCustomers, namedParameters, getRowMapper());
}
public void createPersons(List<Person> customers) {
SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(customers.toArray());
String createCustomer = "INSERT INTO " + tableName + " VALUES(:name)";
namedParameterJdbcTemplate.batchUpdate(createCustomer, params);
}
public void deleteCustomers() {
Map<String, Object> namedParameters = new HashMap<String, Object>();
String deleteCustomers = "DELETE FROM " + tableName;
namedParameterJdbcTemplate.update(deleteCustomers, namedParameters);
}
private static RowMapper<Person> getRowMapper() {
return new RowMapper<Person>() {
#Override
public Person mapRow(ResultSet arg0, int arg1) throws SQLException {
return new Person(arg0.getString("name"));
}
};
}
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private String tableName;
}
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "/beans.xml")
#Transactional(rollbackFor = Exception.class)
public class PersonsDaoTest {
#Resource
private MyInnerClass myInnerClass;
#Resource
private MyOuterClass myOuterClass;
#Test(expected = Exception.class)
public void test() {
myOuterClass.method();
fail();
}
#After
public void after() {
assertEquals(1, myInnerClass.getPersonsDao().getPersons().size());
assertEquals(0, myOuterClass.getPersonsDao().getPersons().size());
}
#Before
public void before() {
myInnerClass.getPersonsDao().deleteCustomers();
myOuterClass.getPersonsDao().deleteCustomers();
assertEquals(0, myInnerClass.getPersonsDao().getPersons().size());
assertEquals(0, myOuterClass.getPersonsDao().getPersons().size());
}
}
First of all, the #Transactional annotations on your two classes are ignored, since you instantiates these classes directly (using new) rather than getting an instance from the spring context.
So in fact, it boild down to this code:
try {
personDao2.createPerson(); // creates a person in persons2
throw new RuntimeException();
}
finally {
personDao1.createPerson(); // creates a person in person1
}
A finally block is always executed, even if an exception is thrown in the try block. So the test creates a person in person1 and in person2.
#Anders Your InnerClass with the #Transactional annotation does not derive from an interface, if you are not using AspectJ weaving or CG-LIB based proxies, the #Transactional aspect won't take effect, as the dynamic proxies require an interface to be present. A quick fix will be to derive your inner class from an interface, define the bean in the spring config and consistently use the interface for referring to the bean.

Categories

Resources